Skip to content


Prevent Duplication of Memory with Php5 Singleton Design Pattern

One of the most used and important base php pattern is singleton.The Singleton pattern is used to improve the performance of your application.

” The main purpose of the Singleton pattern is to deliver a single instance of object  no matter how many times you instantiate it.That is, if an object is instantiated once , using the singleton pattern you can deliver only that instance when you require it again in your code.This saves memory consumption by preventing the creation of multiple instances of an object.” 

(OOP with PHP5 Book - Hasin Hayder - Packt Publishing Page 75.)

If you use a class multiple times in your application  Singleton Pattern  can save your life. It can prevent duplication of memory on your server. 
 i will do an interesting sample how we can use it …

/*
*  An Example of Singleton Design Pattern
*  Ersin Güvenç.
*/

Class MyClass
{
    //we have to declare it as static.
    static $instance;
   
    //this object can not copy
    //except the getInstance() method.
    private function __clone() {}
   
    //prevent directly access
    //we want acces this only from getInstance method.
    private function __construct(){}
   
    //lets prevent duplication of memory using
    //by Singleton Pattern.
    public static function getInstance()
    {
       if(! (self::$instance instanceof self))
       {
        self::$instance = new self();
           
            echo "used new instance\n";
      
       } else {
      
            echo "used old instance \n";
       }
       return self::$instance;
    }
   
    //your other functions can continue ...
    function do_test()
    {
      //im just a function.  
    }
   

} //end of the class.
$a = MyClass::getInstance();
$b = MyClass::getInstance();
$c = MyClass::getInstance();
$d = MyClass::getInstance();

#Output:
// used new instance
// used old instance
// used old instance
// used old instance

 
  • Facebook
  • Digg
  • Delicious
  • Google Bookmarks
  • Technorati Favorites
  • Yahoo Bookmarks
  • Webnews
  • Technotizie
  • Taggly
  • Linkatopia
  • Ping
  • StumbleUpon
  • Twitter
  • Share/Bookmark

Posted in php articles.

Tagged with , , , , , , .


2 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Russ says

    this is nice. My question is if not using singleton, and you instantiate a class inside a function

    function foo(){
    $bar = new MyClass()
    }

    is that class/variable local to the function? Does it ‘disappear’ when the function completes?

  2. admin says

    Yes this will not appear, this is a php base rule you can use return word

    function foo(){
    $bar = new MyClass()
    return $bar;
    }

    // call the foo handle
    var_dump(foo()); // output object

    // get instance way (recommended)
    function foo(){
    rerturn new MyClass::getInstance()->do_test();
    }

    foo(); // do_test output



Some HTML is OK

or, reply to this post via trackback.



Easy AdSense by Unreal