note 68816 added to language.oop5.patterns
| From: | Dennis at osu1 dot php dot net | Date: | Fri, 11 Aug 2006 19:19:48 +0000 |
| Subject: | note 68816 added to language.oop5.patterns | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-115807@lists.php.net to get a copy of this message | ||
An easy way to have your singleton persistent between page loads:
/**
* Returns an instance of the singleton class.
* @return object The singleton instance
*/
public static function _instance()
{
// Start a session if not already started
Session::start();
if ( false == isset( $_SESSION[ self::$_singleton_class ] ) )
{
$class = self::$_singleton_class;
$_SESSION[ self::$_singleton_class ] = new $class;
}
return $_SESSION[ self::$_singleton_class ];
}
/**
* Destroy the singleton object. Deleting the session variable in the
* destructor does not make sense since the destructor is called every
* time the script ends.
*/
public static function _destroy()
{
$_SESSION[ self::$_singleton_class ] = null;
}
/**
* Initialize the singleton object. Use instead of constructor.
*/
public function _initialize( $name )
{
// Something...
}
/**
* Prevent cloning of singleton.
*/
private function __clone()
{
trigger_error( "Cloning a singleton object is not allowed.", E_USER_ERROR );
}
private static $_singleton_class = __CLASS__;
----
Server IP: 66.163.161.117
Probable Submitter: 83.227.150.10
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php
Edit -- https://master.php.net/note/edit/68816
Del: integrated -- https://master.php.net/note/delete/68816/integrated
Del: useless -- https://master.php.net/note/delete/68816/useless
Del: bad code -- https://master.php.net/note/delete/68816/bad+code
Del: spam -- https://master.php.net/note/delete/68816/spam
Del: non-english -- https://master.php.net/note/delete/68816/non-english
Del: in docs -- https://master.php.net/note/delete/68816/in+docs
Del: other reasons-- https://master.php.net/note/delete/68816
Reject -- https://master.php.net/note/reject/68816
Search -- https://master.php.net/manage/user-notes.php