note 41897 deleted from language.oop by aidan

From: Date: Sat, 03 Jul 2004 14:45:41 +0000
Subject: note 41897 deleted from language.oop by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-72431@lists.php.net to get a copy of this message
Note Submitter: steffen at schomberg dot net ---- Here's how to implement the singleton programming pattern using PHP5. <?php // Singleton Pattern in PHP5 class Test { private $num; static protected $instance = null; static public function getTest() { if (Test::$instance == null) { Test::$instance = new Test(); } return Test::$instance; } protected function __construct() { $this->num = rand(0,1000); } public function getNum() { echo "NUM: " . $this->num; } } // call Singleton method instead of constructor $t1 = Test::getTest(); $t2 = Test::getTest(); // $t1 and $t2 reference the same object $t1->getNum(); $t2->getNum(); ?>

« previous php.notes (#72431) next »