note 76235 added to language.oop5.decon

From: Date: Fri, 06 Jul 2007 14:46:41 +0000
Subject: note 76235 added to language.oop5.decon
Groups: php.notes 
Request: Send a blank email to php-notes+get-128268@lists.php.net to get a copy of this message
This is just to clarify that the Singleton pattern is a bit more complex than just making the constructor private. It also involves caching an instance of the object and always returning the cached value. So, in the previous example, the getNewInstance() function would undermine the intent of the Singleton pattern. Instead you would just need a getInstance() function, like so. <?php class A { // cached instance private static oInst = null; /** * Prevent an object from being constructed. */ private function __construct( ) {} /** * Function to return the instance of this class. */ public static function getInstance( ) { if (is_null(self::$oInst)) { self::$oInst = new A( ); } return self::$oInst; } } ?> ---- Server IP: 69.147.83.197 Probable Submitter: 152.16.238.201 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php Edit -- https://master.php.net/note/edit/76235 Del: integrated -- https://master.php.net/note/delete/76235/integrated Del: useless -- https://master.php.net/note/delete/76235/useless Del: bad code -- https://master.php.net/note/delete/76235/bad+code Del: spam -- https://master.php.net/note/delete/76235/spam Del: non-english -- https://master.php.net/note/delete/76235/non-english Del: in docs -- https://master.php.net/note/delete/76235/in+docs Del: other reasons-- https://master.php.net/note/delete/76235 Reject -- https://master.php.net/note/reject/76235 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#128268) next »