Heim  >  Artikel  >  Backend-Entwicklung  >  php设计模式入门-单例模式_PHP教程

php设计模式入门-单例模式_PHP教程

WBOY
WBOYOriginal
2016-07-13 09:45:28779Durchsuche

php设计模式入门-单例模式

在日常开发过程中,常常会遇到有些类是不需要重复实例化的,比如数据库连接,在这种情况下,单例模式就是最佳的解决方案。正好上次面试也有被问及这个模式,今天在这里做个总结。

先来看一个最简单的单例模式的实现:

 

<!--?php
class Singleton{
     static $instance;

     static function getInstance(){
          if(is_null(self::$instance)){
               self::$instance = new self();
          }
          return self::$instance;
     }

}

$test1 = Singleton::getInstance();
$test2 = Singleton::getInstance();

if($test1 === $test2){
     echo 是同一个对象;
}else{
     echo 不是同一个对象;
}
</pre-->

运行结果如下:

 

\

但是光是这样写严格意义上来讲并不是真正的单例模式,因为用户完全可以通过new关键词来实例化新的对象。

 

$test1 = new Singleton();
$test2 = new Singleton();
运行结果如下:

 

\

所以我们要对我们的代码做一点改进,将构造方法的访问级别设置为protected:

 

<!--?php
class Singleton{
     static $instance;

     protected function __construct(){
         
     }

     static function getInstance(){
          if(is_null(self::$instance)){
               self::$instance = new self();
          }
          return self::$instance;
     }

}

$test1 = new Singleton();
$test2 = new Singleton();

if($test1 === $test2){
     echo 是同一个对象;
}else{
     echo 不是同一个对象;
}
</pre-->这时当用户试图以new关键词实例化一个新的对象时,会报如下截图所示的错误:

 

\

当然,狡猾的用户还是可以通过clone关键词来克隆一个新对象:

 

$test1 = Singleton::getInstance();
$test2 = clone $test1;

if($test1 === $test2){
     echo 是同一个对象;
}else{
     echo 不是同一个对象;
}

运行结果:

 

\

所以我们还要把__clone方法声明为protected:

 

<!--?php
class Singleton{
     static $instance;

     protected function __construct(){
         
     }

     static function getInstance(){
          if(is_null(self::$instance)){
               self::$instance = new self();
          }
          return self::$instance;
     }

     protected function __clone(){
         
     }

}

$test1 = Singleton::getInstance();
$test2 = clone $test1;

if($test1 === $test2){
     echo 是同一个对象;
}else{
     echo 不是同一个对象;
}
</pre-->时当用户试图以clone关键词克隆一个新的对象时,会报如下截图所示的错误:

 

\

所以要严格意义上实现一个单例模式,要注意的有以下几点:

1、将构造函数声明为protected;

2、创建一个getInstance的静态方法来获取保存该类的静态变量;

3、将__clone方法声明为protected

当然在实际的开发中,大部分情况下只要实现简单的单例模式就行了(第一个例子的写法)。

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1040917.htmlTechArticlephp设计模式入门-单例模式 在日常开发过程中,常常会遇到有些类是不需要重复实例化的,比如数据库连接,在这种情况下,单例模式就是...
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn