note 108293 added to language.oop5.traits

From: Date: Sun, 15 Apr 2012 02:03:17 +0000
Subject: note 108293 added to language.oop5.traits
Groups: php.notes 
Request: Send a blank email to php-notes+get-187128@lists.php.net to get a copy of this message
Simple singleton trait. <?php trait singleton { /** * private construct, generally defined by using class */ //private function __construct() {} public static function getInstance() { static $_instance = NULL; $class = __CLASS__; return $_instance ?: $_instance = new $class; } public function __clone() { trigger_error('Cloning '.__CLASS__.' is not allowed.',E_USER_ERROR); } public function __wakeup() { trigger_error('Unserializing '.__CLASS__.' is not allowed.',E_USER_ERROR); } } /** * Example Usage */ class foo { use singleton; private function __construct() { $this->name = 'foo'; } } class bar { use singleton; private function __construct() { $this->name = 'bar'; } } $foo = foo::getInstance(); echo $foo->name; $bar = bar::getInstance(); echo $bar->name; ---- Server IP: 192.41.42.26 Probable Submitter: 66.61.77.245 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.traits.php Edit -- https://master.php.net/note/edit/108293 Del: integrated -- https://master.php.net/note/delete/108293/integrated Del: useless -- https://master.php.net/note/delete/108293/useless Del: bad code -- https://master.php.net/note/delete/108293/bad+code Del: spam -- https://master.php.net/note/delete/108293/spam Del: non-english -- https://master.php.net/note/delete/108293/non-english Del: in docs -- https://master.php.net/note/delete/108293/in+docs Del: other reasons-- https://master.php.net/note/delete/108293 Reject -- https://master.php.net/note/reject/108293 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#187128) next »