码迷,mamicode.com
首页 > 其他好文 > 详细

单例模式

时间:2015-04-01 16:54:16      阅读:97      评论: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
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!