Php design-patterns-singleton-design-pattern
Upcoming SlideShare
Loading in...5
×
 

Php design-patterns-singleton-design-pattern

on

  • 3,304 views

How to develop the singleton design pattern class with php

How to develop the singleton design pattern class with php

Statistics

Views

Total Views
3,304
Views on SlideShare
3,303
Embed Views
1

Actions

Likes
1
Downloads
46
Comments
2

1 Embed 1

http://localhost 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

12 of 2

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • for csharp version
    http://www.slideshare.net/jinaldesailive/all-about-singleton-pattern
    Are you sure you want to
    Your message goes here
    Processing…
  • Singleton is broken in so many ways, especially in PHP. Do not use it!
    That beside, you even forgot to mention __clone() and __wakup().

    Please - 'Friends don't let friends do singleton'
    http://www.slideshare.net/frank_sons/the-singleton-dilemma
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Php design-patterns-singleton-design-pattern Php design-patterns-singleton-design-pattern Presentation Transcript

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