PHP单例模式 demo

<?php

class single{

   static public $db;

   private function __construct(){

   };

    static function getinstance(){

         if(!self::$db)

             self::$bd = new self();

       return self::$db;

   }

}

single::getinstance();

//demo 2

class pzhang{

  static private $instance;

  private $config;

  private function __construct($config){

      $this->config = $config;

      echo "我被实例化了";

  }

 private function __clone(){

}

 static public function getInstance($config){

      if(!self::$instance  instanceof self)

           self::$instance = new self($config);

    return self::$instance;

 }

 public function getname(){

   echo $this->config;

}

}

$adb = pzhang::getInstance(1);

$adb->getname();

 

//结果   我被实例化了1

?>

posted @ 2018-10-08 15:13  酸suan  阅读(564)  评论(0编辑  收藏  举报