首页 > Web开发 > 详细

PHP单例模式 demo

时间:2018-10-08 15:44:10      阅读:156      评论:0      收藏:0      [点我收藏+]

<?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

?>

PHP单例模式 demo

原文:https://www.cnblogs.com/isuansuan/p/9754528.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!