Review PHP设计模式之——单例模式

单例模式:

 1 class Single {
 2     private static $_instance;
 3 
 4     private function __construct(){
 5         //define method as private
 6     }
 7 
 8     public function __clone(){
 9         trigger_error('Clone is not allow!', E_USER_ERROR);
10     }
11 
12     public static function getInstance(){
13         if(!(self::$_instance instanceof self)){
14             self::$_instance = new self;
15         }
16         return self::$_instance;
17     }
18 
19     public function show(){
20         phpinfo();
21     }
22 }
23 $single = Single::getInstance();
24 $single->show();

 

posted on 2016-03-11 10:30  Jacky Yu  阅读(248)  评论(0编辑  收藏  举报