首页 > 其他 > 详细

单例模式

时间:2018-07-03 23:00:24      阅读:150      评论:0      收藏:0      [点我收藏+]
<?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]
  

  

单例模式

原文:https://www.cnblogs.com/wangfenphph2/p/9261058.html

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