单例模式

<?php
   
   class   DB{
      //私有属性,用来保存单例;
   	 private   static $instance;
   	 //私有构造函数,阻止在类的外部实例化
   	 private   function  __construct(){

   	 }
     //私有克隆函数,阻止在类的外部克隆对象;
     private   function  __clone(){

     }
     //公有方法用来获取单例;
     public    function  getInstance(){
     //当前对象不属于当前类的实例;
        if(! self :: $instance  instanceof self)
        	 self::$instance=new  self;
        	 return  self::$instance;
     }
   }
   $object1=DB::getInstance();
   $object2=DB::getInstance();
   var_dump($object1,$object2);
   //object(DB)[1]
  

  

posted @ 2018-07-03 22:42  王芬老师  阅读(115)  评论(0编辑  收藏  举报