note 93677 added to language.oop5.patterns
| From: | ltearno at gmail dot com | Date: | Tue, 22 Sep 2009 16:11:02 +0000 |
| Subject: | note 93677 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-160942@lists.php.net to get a copy of this message | ||
The Singleton class version wich is useable by only extending it :
you would use is for example like this :
class Users extends Singleton
{
...
public function SomeMethod()
{
}
}
and in your code :
...
Users::inst()->SomeMethod();
...
I didn't check for performance, but here is the code :
class Singleton
{
// Hold an instance of the class
private static $instances = array();
// A private constructor; prevents direct creation of object
final private function __construct()
{
}
// Prevent users to clone the instance
final public function __clone()
{
trigger_error( 'Clone is not allowed.', E_USER_ERROR );
}
// The singleton method
final public static function inst()
{
$c = get_called_class();
if( ! isset( self::$instances[$c] ) )
{
self::$instances[$c] = new $c;
}
return self::$instances[$c];
}
}
----
Server IP: 89.31.146.176
Probable Submitter: 92.143.11.74
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/93677
Del: integrated -- https://master.php.net/note/delete/93677/integrated
Del: useless -- https://master.php.net/note/delete/93677/useless
Del: bad code -- https://master.php.net/note/delete/93677/bad+code
Del: spam -- https://master.php.net/note/delete/93677/spam
Del: non-english -- https://master.php.net/note/delete/93677/non-english
Del: in docs -- https://master.php.net/note/delete/93677/in+docs
Del: other reasons-- https://master.php.net/note/delete/93677
Reject -- https://master.php.net/note/reject/93677
Search -- https://master.php.net/manage/user-notes.php