note 82727 added to language.oop5.late-static-bindings
| From: | php at mikebird dot co dot uk | Date: | Wed, 23 Apr 2008 15:39:48 +0000 |
| Subject: | note 82727 added to language.oop5.late-static-bindings | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-138702@lists.php.net to get a copy of this message | ||
This should make life easier and neater if you have a project with a lot of singleton classes e.g.
<?php
class Singleton {
public static $objInstance;
public static function &getInstance() {
if (self::$objInstance == null) {
$strClass = static::getClass();
self::$objInstance = new $strClass;
}
return self::$objInstance;
}
public static function getClass() {
return __CLASS__;
}
}
class Foo extends Singleton {
public $intBar;
public function __construct() {
$this->intBar = 1;
}
public static function getClass() {
return __CLASS__;
}
}
$objFooTwo = Foo::getInstance();
$objFooTwo->intBar = 2;
$objFooOne = Foo::getInstance();
if ($objFooOne->intBar == $objFooTwo->intBar) {
echo 'it is a singleton';
} else {
echo 'it is not a singleton';
}
?>
The above will output 'it is a singleton'. The obvious downfall to this method is not
being able to give arguments to the constructor.
----
Server IP: 87.124.64.96
Probable Submitter: 91.84.128.149
----
Manual Page -- http://www.php.net/manual/en/language.oop5.late-static-bindings.php
Edit -- https://master.php.net/note/edit/82727
Del: integrated -- https://master.php.net/note/delete/82727/integrated
Del: useless -- https://master.php.net/note/delete/82727/useless
Del: bad code -- https://master.php.net/note/delete/82727/bad+code
Del: spam -- https://master.php.net/note/delete/82727/spam
Del: non-english -- https://master.php.net/note/delete/82727/non-english
Del: in docs -- https://master.php.net/note/delete/82727/in+docs
Del: other reasons-- https://master.php.net/note/delete/82727
Reject -- https://master.php.net/note/reject/82727
Search -- https://master.php.net/manage/user-notes.php