欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

Review PHP设计模式之单例模式 - Jacky Yu

程序员文章站 2022-05-20 16:50:21
...
单例模式:
 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();