Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Php design-patterns-singleton-design-pattern


Published on

How to develop the singleton design pattern class with php

Published in: Technology

Php design-patterns-singleton-design-pattern

  1. 1. Singleton Design Pattern by Design Patterns in PHP
  2. 2. Table of Contents Introduction to Singleton Design Pattern Definition of Singleton Design Pattern Conclusion Process Inside Singleton Creating a Singleton Class Step by Step Whats Next ? 6 5 4 3 2 1
  3. 3. <ul><li>Singleton design pattern is the most simple design pattern which has only one class. </li></ul><ul><li>Singleton design pattern allows only one object to be created from a singleton class. </li></ul><ul><li>If many other classes need to use a object from a singleton class, they have to use the same instance. </li></ul><ul><li>These types of classes are required for logging, registry, database connections, memory manager, window manager…etc. </li></ul><ul><li>This allows many programmers to use this class using the same instance. </li></ul><ul><li>This procedure works like a static field in a class. </li></ul><ul><li>When we create a static variable and initialize it, all other objects which use this variable access the same instance. </li></ul>Introduction to Singleton Design Pattern
  4. 4. <ul><li>In lower level what happens is that only one object or variable is created in the memory. </li></ul><ul><li>All other objects points to that memory location, to access the static object or variable instead of creating separate memory locations for separate variables. </li></ul>Process Inside Singleton
  5. 5. <ul><li>Singleton pattern is a design pattern that is used to restrict instantiation of a class to one object (an implementation of the mathematical concept of singleton). This is useful when exactly one object is needed to coordinate actions across the system </li></ul><ul><ul><li>[Ref : http:// ] </li></ul></ul><ul><li>Ensure a class has only one instance, and provide global point of access to it. </li></ul><ul><ul><li>[Ref : http:// ] </li></ul></ul>Definition of Singleton Design Pattern
  6. 6. <ul><li>Step 1 </li></ul><ul><ul><li>We need is a class that shares the same object instance in all requests. First we create a normal PHP class . </li></ul></ul><ul><ul><li>class singleton{ </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Step 2 </li></ul><ul><ul><li>We don’t want to create multiple objects of Singleton using other classes. </li></ul></ul><ul><ul><li>As long as we have a class with public constructor we can create objects. </li></ul></ul><ul><ul><li>So we have to make the constructor private. </li></ul></ul><ul><ul><li>Then only class that can call the constructor is the singleton class. </li></ul></ul><ul><li>class Singleton{ </li></ul><ul><li>private function __construct(){ </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Creating a Singleton Class Step by Step
  7. 7. <ul><li>Step 3 </li></ul><ul><ul><li>Now the only way of creating a object of Singleton class is to call the constructor within the class. </li></ul></ul><ul><ul><li>We need another function to do that. Inside that function we create the object. </li></ul></ul><ul><li>class Singleton{ </li></ul><ul><li>private function __construct(){ </li></ul><ul><li>} </li></ul><ul><li>public function getInstance(){ </li></ul><ul><li>$singleton=new Singleton(); </li></ul><ul><li>} </li></ul><ul><ul><li>} </li></ul></ul>Creating a Singleton Class Step by Step
  8. 8. <ul><li>Step 4 </li></ul><ul><ul><li>Now all other classes which need object of singleton class, has to call the getInstance method. </li></ul></ul><ul><ul><li>Since we can’t create objects of Singleton directly, we can’t call getInstance function. </li></ul></ul><ul><ul><li>So we have to make the getInstance function static. </li></ul></ul><ul><ul><li>Then we can call it using the class name. </li></ul></ul><ul><li>class Singleton{ </li></ul><ul><li>private function __construct(){ </li></ul><ul><li>} </li></ul><ul><li>public static function getInstance(){ </li></ul><ul><li>$singleton=new Singleton(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Creating a Singleton Class Step by Step
  9. 9. <ul><li>Step 5 </li></ul><ul><ul><li>Now we can create a object from Singleton class. </li></ul></ul><ul><ul><li>But the problem is we can create many objects. </li></ul></ul><ul><ul><li>We can’t limit the number of objects to one. </li></ul></ul><ul><ul><li>So we have to create a variable to hold the Singleton instance. </li></ul></ul><ul><ul><li>We have restricted other classes using this variable directly by making the variable private. </li></ul></ul><ul><li>class Singleton{ </li></ul><ul><li>private $instance; </li></ul><ul><li>private function __construct(){ </li></ul><ul><li>} </li></ul><ul><li>public static function getInstance(){ </li></ul><ul><li>$singleton=new Singleton(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Creating a Singleton Class Step by Step
  10. 10. <ul><li>Step 6 </li></ul><ul><ul><li>Since we are using $instance inside a static function we have to make $instance static. </li></ul></ul><ul><li>class Singleton{ </li></ul><ul><li>private static $instance; </li></ul><ul><li>private function __construct(){ </li></ul><ul><li>} </li></ul><ul><li>public static function getInstance(){ </li></ul><ul><li>$singleton=new Singleton(); </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Creating a Singleton Class Step by Step
  11. 11. <ul><li>Step 7 </li></ul><ul><ul><li>In order to restrict creation of objects to a single object we have to check whether object already exist . </li></ul></ul><ul><ul><li>If there is a initialized object we return the same instance. </li></ul></ul><ul><ul><li>If not we can create a new object. </li></ul></ul><ul><ul><li>Then all classes will use the same object. </li></ul></ul><ul><li>class Singleton{ </li></ul><ul><li>private static $instance; </li></ul><ul><li>private function __construct(){ </li></ul><ul><li>} </li></ul><ul><li>public static function getInstance(){ </li></ul><ul><li>If($instance!=null){ </li></ul><ul><li>$instance=new Singleton(); </li></ul><ul><li>} </li></ul><ul><li>return $singleton; </li></ul><ul><li>} </li></ul><ul><li>} </li></ul>Creating a Singleton Class Step by Step
  12. 12. <ul><li>Step 8 </li></ul><ul><ul><li>Now we can use the Singleton class to create a one instance and share the instance between many requests. </li></ul></ul><ul><li>$singleton=Singleton::getInstance(); </li></ul>Creating a Singleton Class Step by Step
  13. 13. <ul><li>Singleton design pattern is a effective way to restrict creation of objects to a limited number. </li></ul><ul><li>This is a very effective way to use manage extensive usage of resources such as Database connections and memory. </li></ul><ul><li>Step by step process of creating singleton class is provided in this article. This article series will continue to provide details of all the design patterns. </li></ul>Conclusion
  14. 14. What's Next ? <ul><li>You can view more great tutorials and articles at </li></ul><ul><li>New on InnovativePhp </li></ul><ul><ul><li>How to Create a Simple Tab Based Navigation With Jquery . </li></ul></ul><ul><ul><li>How To Convert Key-Value Pairs To An Object Using PHP . </li></ul></ul><ul><ul><li>Wrapping Text With PHP And CSS – Formating Content . </li></ul></ul><ul><ul><li>Creating a Pagination Library From Scratch . </li></ul></ul><ul><ul><li>CodeIgniter Form Validation – Using CodeIgniter Validation Library     </li></ul></ul>