note 90090 added to language.oop5.patterns
| From: | grummfy at gmail dot com | Date: | Mon, 06 Apr 2009 03:33:21 +0000 |
| Subject: | note 90090 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-152849@lists.php.net to get a copy of this message | ||
Exemple of singleton that can be used for other class (need phpversion >= 5.3)
<?php
class SingletonModel
{
/**
* Instance of singleton's class
*
* @var array
*/
private static $_instance;
/**
* All constructeur should be protected
*/
protected function __construct(){}
/**
* Singleton method to load a new object
*
* @return HlPwd_SingletonModel
*/
public static function singleton()
{
if (!isset(self::$instance[ get_called_class() ]))
{
$c = get_called_class();
self::$instance = new $c;
}
return self::$instance;
}
/**
* Destroy the singleton
*/
public function destroySingleton()
{
unset(self::$_instance[ get_class($this) ]);
}
/**
* Destructeur
*/
public function __destruct()
{
unset(self::$_instance);
}
/**
* Impeach the clone of singleton
*/
public function __clone()
{
trigger_error('Cloning not allowed on a singleton object', E_USER_ERROR);
}
}
# EOF
you can easyli use it like that
class mySingChild extends SingletonModel
{
protected function __construct()
{
//do important things
}
public function foo()
{
echo 'hello world';
}
}
$t = mySingChild::singleton();
$t->foo();
----
Server IP: 195.207.10.20
Probable Submitter: 83.134.209.76
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/90090
Del: integrated -- https://master.php.net/note/delete/90090/integrated
Del: useless -- https://master.php.net/note/delete/90090/useless
Del: bad code -- https://master.php.net/note/delete/90090/bad+code
Del: spam -- https://master.php.net/note/delete/90090/spam
Del: non-english -- https://master.php.net/note/delete/90090/non-english
Del: in docs -- https://master.php.net/note/delete/90090/in+docs
Del: other reasons-- https://master.php.net/note/delete/90090
Reject -- https://master.php.net/note/reject/90090
Search -- https://master.php.net/manage/user-notes.php