Creating the Singleton design pattern in PHP5

前端 未结 21 1744
猫巷女王i
猫巷女王i 2020-11-22 04:21

How would one create a Singleton class using PHP5 classes?

相关标签:
21条回答
  • 2020-11-22 05:14
    <?php
    /**
     * Singleton patter in php
     **/
    trait SingletonTrait {
       protected static $inst = null;
    
      /**
       * call this method to get instance
       **/
       public static function getInstance(){
          if (static::$inst === null){
             static::$inst = new static();
          }
          return static::$inst;
      }
    
      /**
       * protected to prevent clonning 
       **/
      protected function __clone(){
      }
    
      /**
       * protected so no one else can instance it 
       **/
      protected function __construct(){
      }
    }
    

    to use:

    /**
     *  example of class definitions using SingletonTrait
     */
    class DBFactory {
      /**
       * we are adding the trait here 
       **/
       use SingletonTrait;
    
      /**
       * This class will have a single db connection as an example
       **/
      protected $db;
    
    
     /**
      * as an example we will create a PDO connection
      **/
      protected function __construct(){
        $this->db = 
            new PDO('mysql:dbname=foodb;port=3305;host=127.0.0.1','foouser','foopass');
      }
    }
    class DBFactoryChild extends DBFactory {
      /**
       * we repeating the inst so that it will differentiate it
       * from UserFactory singleton
       **/
       protected static $inst = null;
    }
    
    
    /**
     * example of instanciating the classes
     */
    $uf0 = DBFactoryChild::getInstance();
    var_dump($uf0);
    $uf1 = DBFactory::getInstance();
    var_dump($uf1);
    echo $uf0 === $uf1;
    

    respose:

    object(DBFactoryChild)#1 (0) {
    }
    object(DBFactory)#2 (0) {
    }
    

    If you are using PHP 5.4: trait its an option, so you don't have to waste the inheritance hierarchy in order to have the Singleton pattern

    and also notice that whether you use traits or extends Singleton class one loose end was to create singleton of child classes if you dont add the following line of code:

       protected static $inst = null;
    

    in the child class

    the unexpected result will be:

    object(DBFactoryChild)#1 (0) {
    }
    object(DBFactoryChild)#1 (0) {
    }
    
    0 讨论(0)
  • 2020-11-22 05:14

    This should be the right way of Singleton.

    class Singleton {
    
        private static $instance;
        private $count = 0;
    
        protected function __construct(){
    
        }
    
        public static function singleton(){
    
            if (!isset(self::$instance)) {
    
                self::$instance = new Singleton;
    
            }
    
            return self::$instance;
    
        }
    
        public function increment()
        {
            return $this->count++;
        }
    
        protected function __clone(){
    
        }
    
        protected function __wakeup(){
    
        }
    
    } 
    
    0 讨论(0)
  • 2020-11-22 05:16

    This is the example of create singleton on Database class

    design patterns 1) singleton

    class Database{
      public static $instance;
      public static function getInstance(){
        if(!isset(Database::$instance)){
        Database::$instance=new Database();
    
         return Database::$instance;
        }
    
      }
    
      $db=Database::getInstance();
      $db2=Database::getInstance();
      $db3=Database::getInstance();
    
      var_dump($db);
      var_dump($db2);
      var_dump($db3);
    

    then out put is --

      object(Database)[1]
      object(Database)[1]
      object(Database)[1]
    

    use only single instance not create 3 instance

    0 讨论(0)
提交回复
热议问题