note 91808 added to language.oop5.patterns

From: Date: Fri, 26 Jun 2009 10:44:40 +0000
Subject: note 91808 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-157523@lists.php.net to get a copy of this message
@rskret at gmail dot com i dont think that your "correction" will work as you expect <?php class Example { private static $instance; protected $notice = array(); protected function __construct() { echo 'I am constructed'; } // The singleton method public static function singleton() { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } public function setNotice( $string ) { $this->notice[$string] = true; } public function getNotice() { return array_keys($this->notice); } } class Example2 extends Example { // Hold an instance of the class private static $instance; public static function singleton() { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; return self::$instance; } echo "Sorry but I'm already set."; } } // This will always retrieve a single instance of the class $test = Example::singleton(); #Output: I am constructed $test->setNotice('Milk'); $test->setNotice('Bread'); $test->setNotice('Eggs'); var_dump($test); #Output: /* * object(Example)[1] protected 'notice' => array 'Milk' => boolean true 'Bread' => boolean true 'Eggs' => boolean true */ // overwrite $test $test = 'Oh noes!'; var_dump($test); #Output: /* string 'Oh noes!' (length=8) */ $recover = Example::singleton(); var_dump($recover); #Output: /* * object(Example)[1] protected 'notice' => array 'Milk' => boolean true 'Bread' => boolean true 'Eggs' => boolean true */ unset($test, $recover); $test = Example2::singleton(); #Output: I am constructed $test->setNotice('Milk'); $test->setNotice('Bread'); $test->setNotice('Eggs'); var_dump($test); #Output: /* * object(Example2)[2] protected 'notice' => array 'Milk' => boolean true 'Bread' => boolean true 'Eggs' => boolean true */ // overwrite $test $test = 'Oh noes!'; var_dump($test); #Output: /* string 'Oh noes!' (length=8) */ $recover = Example2::singleton(); #Output: Sorry but I'm already set. var_dump($recover); #Output: /* null */ ?> I know a lame example. But if you want to use (and thats whats a singleton is for) in *n nummbers of classes you need to "recover" your instance. ---- Server IP: 209.41.74.194 Probable Submitter: 84.144.64.63 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/91808 Del: integrated -- https://master.php.net/note/delete/91808/integrated Del: useless -- https://master.php.net/note/delete/91808/useless Del: bad code -- https://master.php.net/note/delete/91808/bad+code Del: spam -- https://master.php.net/note/delete/91808/spam Del: non-english -- https://master.php.net/note/delete/91808/non-english Del: in docs -- https://master.php.net/note/delete/91808/in+docs Del: other reasons-- https://master.php.net/note/delete/91808 Reject -- https://master.php.net/note/reject/91808 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#157523) next »