note 91309 added to language.oop5.patterns

From: Date: Thu, 04 Jun 2009 22:46:56 +0000
Subject: note 91309 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-156376@lists.php.net to get a copy of this message
Ex.2 only makes a 'Singleton' in the sense there is only one object makable. But! you can still assign the object to other names(like making aliases). Probably not what you usually want. Change the method a bit and you get only one unique name for your 'Singleton'.. or else it will bark again and again! <?php #if you don't change example 2 you will get a $badDoggie $badDoggie="I am constructedSorry but I'm already set.Sorry but I'm already set. Sorry but I'm already set.Woof!Woof!Woof!"; // The singleton method public static function singleton() { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; #return self::$instance; } echo "Sorry but I'm already set."; return self::$instance; ##...wrong place for this!comment this, uncomment it above } ?> <?php // This would fail because the constructor is private #$test = new Example; // This will always retrieve a single instance of the class $test = Example::singleton(); $again = Example::singleton(); $and_again = Example::singleton(); $test->bark(); $again->bark(); $and_again->bark(); ?> ---- Server IP: 69.147.83.197 Probable Submitter: 24.68.193.59 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/91309 Del: integrated -- https://master.php.net/note/delete/91309/integrated Del: useless -- https://master.php.net/note/delete/91309/useless Del: bad code -- https://master.php.net/note/delete/91309/bad+code Del: spam -- https://master.php.net/note/delete/91309/spam Del: non-english -- https://master.php.net/note/delete/91309/non-english Del: in docs -- https://master.php.net/note/delete/91309/in+docs Del: other reasons-- https://master.php.net/note/delete/91309 Reject -- https://master.php.net/note/reject/91309 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#156376) next »