Re: Re: PHP5 Static Object Function Issue

From: Date: Wed, 02 Mar 2005 00:01:12 +0000
Subject: Re: Re: PHP5 Static Object Function Issue
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-209758@lists.php.net to get a copy of this message
Well yes and no, conceivably, the system has no idea what singletons are out there other than what is specified in a configuration file. You are in essence hard coding the class name in the switch. Gavin On Tue, 01 Mar 2005 17:47:24 -0500, Jason Barnett <jason.barnett@telesuite.com> wrote: > A switch statement can accomplish what you seek. > > <?php > > class MyClass { > > static $instance; > > protected function __construct() {} > > static function getInstance() { > if (!self::$instance) { > self::$instance = new MyClass(); > } > return self::$instance; > } > > } > > function getSingleton($class_singleton) { > switch(strtoupper($class_singleton)) { > case 'MYCLASS': > $singleton = MyClass::getInstance(); > break; > default: > $singleton = "Unable to load singleton for $class_singleton"; > break; > } > return $singleton; > } > > for ($i = 0; $i <10; $i++) { > $class = ((3 > $i) ? 'MyClass' : null); > $singleton[] = getSingleton($class); > } > > var_dump($singleton); > > ?> > > -- > Teach a man to fish... > > NEW? | http://www.catb.org/~esr/faqs/smart-questions.html > STFA | http://marc.theaimsgroup.com/?l=php-general&w=2 > STFM | http://php.net/manual/en/index.php > STFW | http://www.google.com/search?q=php > LAZY | > > http://mycroft.mozdev.org/download.html?name=PHP&submitform=Find+search+plugins > > >

« previous php.general (#209758) next »