Singleton Objects

We will implement a number of objects for our web application samples, of which we will only ever require one instance. If we have a large object that manages a broad feature area (management of users, accounts, or orders), we will have little need for multiple instances of such. If these large objects manage some sort of state or connection to the database, we might indeed require that only one exist at a time to help preserve consistency or resources (including memory).

Although we could just implement them with a public constructor and instantiate them as follows

 <?php class OnlyNeedOne {   ...   ..   . } $ono = new OnlyNeedOne(); ?> 

this leaves us exposed to problems later. Absolutely nothing prevents a developer from creating a second instance of this class and working with that new instance.

To solve this problem, we will design this object such that you can only create a single instance of it. Computer science jargon refers to this as a singleton object. This terminology came about because of the work of four doctoral students (Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides) who wrote a book identifying some of the most common patterns in object-oriented programming they saw over the years. Their book, Design Patterns: Elements of Reusable Object-Oriented Software (Addison Wesley Professional, 1995), laid the groundwork for the study of patterns in computer science.

To restrict how people create instances of our object, we can first make sure that our constructor is marked as private, which means that the new operator will not work for them:

 <?php class OnlyNeedOne {   private function __construct()   {   } } // // won't work!! // $ono = new OnlyNeedOne(); ?> 

Unfortunately, now the user has no way of creating this object, which might be described as problematic. In fact, the only one who can create an instance is the object itself! We therefore expose a public static method on that class in which it does the object creation for us:

 <?php class OnlyNeedOne {   private function __construct()   {   }   public static function getInstance()   {     return new OnlyNeedOne();   } } // // creates new instance and assigns it to $ono. // $ono = OnlyNeedOne::getInstance(); ?> 

Our final problem is to ensure that users can create only one instance. We do this by creating a static member variable on the class and having our getInstance method check to see whether that is already set. If it is not, the method creates an instance of the object and assigns it to the static member variable. After this is done, the method returns the stored instance, as follows:

 <?php class OnlyNeedOne {   //   // holds the single instance of this class.   //   private static $s_instance;   private function __construct()   {   }   public static function getInstance()   {     if (self::$s_instance === NULL)     {       self::$s_instance = new OnlyNeedOne();     }     return self::$s_instance;   } } // // returns an instance and sets it to $ono. // $ono = OnlyNeedOne::getInstance(); ?> 

For many of the larger objects we create in our sample web applications, such as a user manager or shopping cart manager, we will implement them as singleton objects so that we have only one instance of them. This results in less overhead (less objects to create in memory) and ensures that we are always dealing with the same object and associated data structures.

Core Web Application Development With PHP And MYSQL
Core Web Application Development with PHP and MySQL
ISBN: 0131867164
EAN: 2147483647
Year: 2005
Pages: 255

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: