note 81295 added to language.oop5.patterns

From: Date: Thu, 21 Feb 2008 16:16:07 +0000
Subject: note 81295 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-136356@lists.php.net to get a copy of this message
This allows for singleton functionality via class extension, and does not use any shady functions like eval. <?php interface ISingleton { public static function GetInstance(); } class Singleton implements ISingleton { public static function GetInstance($class='Singleton') { static $instances = array(); if (!is_string($class)) { return null; } if ($instances[$class] === null) { $instances[$class] = new $class; } return $instances[$class]; } } class Mars extends Singleton implements ISingleton { public static function GetInstance() { return Singleton::GetInstance(get_class()); } } class Neptune extends Singleton implements ISingleton { public static function GetInstance() { return Singleton::GetInstance(get_class()); } } $x = Mars::GetInstance(); echo '<pre>'.print_r($x,true).'</pre>'; $x = Neptune::GetInstance(); echo '<pre>'.print_r($x,true).'</pre>'; ?> ---- Server IP: 209.41.74.194 Probable Submitter: 74.93.222.86 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/81295 Del: integrated -- https://master.php.net/note/delete/81295/integrated Del: useless -- https://master.php.net/note/delete/81295/useless Del: bad code -- https://master.php.net/note/delete/81295/bad+code Del: spam -- https://master.php.net/note/delete/81295/spam Del: non-english -- https://master.php.net/note/delete/81295/non-english Del: in docs -- https://master.php.net/note/delete/81295/in+docs Del: other reasons-- https://master.php.net/note/delete/81295 Reject -- https://master.php.net/note/reject/81295 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136356) next »