首页 > 其他 > 详细

单例模式

时间:2015-04-01 16:51:24      阅读:125      评论:0      收藏:0      [点我收藏+]
 1   class Singleton {
 2 
 3       private static $_instance = null;
 4       private $name;
 5     private function __construct(){
 6          $this->name = mt_rand(1,9);
 7     }
 8     public static function getInstance(){
 9         if(self::$_instance == null){
10             self::$_instance = new Singleton();
11         }
12         return self::$_instance;
13     }
14     public function getName(){
15         return $this->name;
16     }
17 
18 }
19     $obj1 = Singleton::getInstance();
20     $obj2 = Singleton::getInstance();
21     //var_dump($obj1);
22     echo $obj1->getName() . "\n";
23     echo $obj2->getName() . "\n";            

 

单例模式

原文:http://www.cnblogs.com/Super-Man/p/4384005.html

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