note 86452 added to language.oop5.static

From: Date: Mon, 20 Oct 2008 08:06:00 +0000
Subject: note 86452 added to language.oop5.static
Groups: php.notes 
Request: Send a blank email to php-notes+get-145499@lists.php.net to get a copy of this message
Hi, here's my simple Singleton example, i think it can be useful for someone. You can use this pattern to connect to the database for example. <?php class MySingleton { private static $instance = null; private function __construct() { $this-> name = 'Freddy'; } public static function getInstance() { if(self::$instance == null) { print "Object created!<br>"; self::$instance = new self; } return self::$instance; } public function sayHello() { print "Hello my name is {$this-> name}!<br>"; } public function setName($name) { $this-> name = $name; } } // $objA = MySingleton::getInstance(); // Object created! $objA-> sayHello(); // Hello my name is Freddy! $objA-> setName("Alex"); $objA-> sayHello(); // Hello my name is Alex! $objB = MySingleton::getInstance(); $objB-> sayHello(); // Hello my name is Alex! $objB-> setName("Bob"); $objA-> sayHello(); // Hello my name is Bob! ?> ---- Server IP: 91.189.86.54 Probable Submitter: 78.107.255.44 (proxied: 78.107.255.44) ---- Manual Page -- http://www.php.net/manual/en/language.oop5.static.php Edit -- https://master.php.net/note/edit/86452 Del: integrated -- https://master.php.net/note/delete/86452/integrated Del: useless -- https://master.php.net/note/delete/86452/useless Del: bad code -- https://master.php.net/note/delete/86452/bad+code Del: spam -- https://master.php.net/note/delete/86452/spam Del: non-english -- https://master.php.net/note/delete/86452/non-english Del: in docs -- https://master.php.net/note/delete/86452/in+docs Del: other reasons-- https://master.php.net/note/delete/86452 Reject -- https://master.php.net/note/reject/86452 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#145499) next »