单例模式

 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";            

 

posted @ 2015-04-01 15:31  起个名字七个字  阅读(175)  评论(0)    收藏  举报