note 101982 added to language.oop5.patterns

From: Date: Fri, 21 Jan 2011 14:27:17 +0000
Subject: note 101982 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-175963@lists.php.net to get a copy of this message
The problem with the singleton pattern is that there aren't really many cases where it is a true singleton. There are many cases where you want to get access to a specific object that is the same each time. For example accessing objects that have different drivers. In this instance its better to use the Lazy Initialization pattern: <?php abstract function Lazy_Initialization_Object { /* Set up the array of instances */ protected static $instances = array(); /** Make sure the constructor isn't accessible to * the rest of the world */ private function __construct() {} /** * Returns a specific object that relies on the * parameter passed * * @param string class name to be returned * @throws Exception if $type doesn't extend __CLASS__ */ public function get_object($type) { $reflection_class = new ReflectionClass($type); // Check if the object is a Lazy Iinitialization Object if( ! $reflection_class->isSubclassOf(__CLASS__)) throw Exception("$type does not extend __CLASS__"); // Check if the object already exists in the pool if( ! array_key_exists($type,self::$instances)) { // If not initialise a new one and place it in the pool self::$instances[$type]=$reflection_class->newInstance(); } // Return the object you want return self::$instances[$type] } /** Specify all the functions that these drivers have in common */ public abstract function initialize($name); public function display() { echo $content } protected $content; } class Person_Driver_HTML extends Lazy_Initialization_Object { public function initialize($name) { $content = "<p>Name: $name</p>"; } } class Person_Driver_Raw extends Lazy_Initialization_Object { public function initialize($name) { $content = "name: $name"; } } Lazy_Initialization_Object:: get_object("Person_Driver_Raw")->initialize("fifi"); Lazy_Initialization_Object:: get_object("Person_Driver_HTML")->initialize("fifi"); Lazy_Initialization_Object:: get_object("Person_Driver_HTML")->display(); // will output "<p>Name: fifi</p>" Lazy_Initialization_Object:: get_object("Person_Driver_Raw")->display(); // will output "name: fifi"; echo Lazy_Initialization_Object:: get_object("Person_Driver_HTML") === Lazy_Initialization_Object:: get_object("Person_Driver_HTML"); // TRUE echo Lazy_Initialization_Object:: get_object("Person_Driver_HTML") === Lazy_Initialization_Object:: get_object("Person_Driver_Raw"); // FALSE ?> ---- Server IP: 69.147.83.197 Probable Submitter: 82.18.86.179 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/101982 Del: integrated -- https://master.php.net/note/delete/101982/integrated Del: useless -- https://master.php.net/note/delete/101982/useless Del: bad code -- https://master.php.net/note/delete/101982/bad+code Del: spam -- https://master.php.net/note/delete/101982/spam Del: non-english -- https://master.php.net/note/delete/101982/non-english Del: in docs -- https://master.php.net/note/delete/101982/in+docs Del: other reasons-- https://master.php.net/note/delete/101982 Reject -- https://master.php.net/note/reject/101982 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#175963) next »