cortex is right - it took me hours to figure it out. None
the less it's sometimes useful to let one sigleton class
extend another. You can achieve this by only using static
attributes:
<?php
class A {
private static $value = 0;
private static $instance = null;
private function __construct() {
$this->set(time());
}
public static function getInstance() {
if (is_null((self::$instance))) {
$class_name = __CLASS__;
self::$instance = new $class_name;
}
return self::$instance;
}
private function set($i) {
self::$value = $i;
$this->out();
}
public function out() {
echo self::$value;
}
}
class B extends A {
public static $instance = null;
public static function getInstance() {
parent::getInstance();
if (is_null(self::$instance)) {
$class = __CLASS__;
self::$instance = new $class;
}
return self::$instance;
}
}
$b = B::getInstance();
$b->out();
?>
This will output the current time twice. If $value isn't
static, the call to $b->out() will output 0. Maybe it can
save someone some time...
----
Server IP: 217.13.201.10
Probable Submitter: 89.53.219.149
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
a>
Edit -- https://master
.php.net/note/edit/70025
Del: integrated -- h
ttps://master.php.net/note/delete/70025/integrated
Del: useless -- http
s://master.php.net/note/delete/70025/useless
Del: bad code -- htt
ps://master.php.net/note/delete/70025/bad+code
Del: spam -- https:/
/master.php.net/note/delete/70025/spam
Del: non-english --
https://master.php.net/note/delete/70025/non-english
Del: in docs -- http
s://master.php.net/note/delete/70025/in+docs
Del: other reasons-- https://mast
er.php.net/note/delete/70025
Reject -- https://mast
er.php.net/note/reject/70025
Search -- https://
master.php.net/manage/user-notes.php
--
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php
|