Home  >  Article  >  Backend Development  >  Detailed explanation of singleton pattern in PHP

Detailed explanation of singleton pattern in PHP

小云云
小云云Original
2018-03-27 16:08:072157browse

This article mainly shares with you a detailed explanation of the singleton mode in PHP. In fact, the singleton mode is easy to understand. As the name suggests, the singleton mode has only one instance. As an object creation mode, the singleton mode ensures that a certain class has only one instance. And instantiate it yourself and provide this instance to the entire system.

Three key points of the singleton pattern:

1. A class can only have one instance.

 2. You must create this instance yourself.

 3. You must provide this instance to the entire system yourself.

Why use PHP singleton mode?

1. A major aspect of PHP applications is for databases. There will be a large number of database operations in an application. When developing in an object-oriented way, if you use the singleton mode, you can avoid a large number of database operations. The resources consumed by the new operation can also reduce database connections, so that too many connections are less likely to occur.

 2. If a class is needed to globally control certain configuration information in a system, then it can be easily implemented using the singleton mode.

 3. It is easy to debug in a page request, because all the code is concentrated in one class, you can set hooks in the class, output logs, and avoid var_dump() and echo everywhere.

This example is enough to deeply understand the magic of the single interest mode:

<?php/**
 * 设计模式之单例模式
 * $_instance必须声明为静态的私有变量
 * 构造函数必须声明为私有,防止外部程序new类从而失去单例模式的意义
 * getInstance()方法必须设置为公有的,必须调用此方法以返回实例的一个引用
 * ::操作符只能访问静态变量和静态函数
 * new对象都会消耗内存
 * 使用场景:最常用的地方是数据库连接。
 * 使用单例模式生成一个对象后,该对象可以被其它众多对象所使用。 */class Girlfriend
{    //保存例实例在此属性中
    private static $_instance;    //构造函数声明为private,防止直接创建对象
    private function __construct()
    {        echo &#39;实例初始化了!&#39;;
    }    //单例方法
    public static function get_instance()
    {        var_dump(isset(self::$_instance));        if(!isset(self::$_instance))
        {
            self::$_instance=new self();
        }        return self::$_instance;
    }    //阻止用户复制对象实例
    private function __clone()
    {        trigger_error(&#39;禁止克隆&#39; ,E_USER_ERROR);
    }    function test()
    {        echo("here is a test");
    }
}// 这个写法会出错,因为构造方法被声明为private
//$test = new Girlfriend;
// 下面将得到该类的单例对象$test = Girlfriend::get_instance();$test = Girlfriend::get_instance();$test->test();// 复制对象将导致一个E_USER_ERROR.
//$test_clone = clone $test;

The above two calls: Girlfriend::get_instance() and the constructor is only called once in the initial instance, and the initial var_dump (isset(self::$_instance)) returns false, and the constructor is not output for multiple subsequent instances...var_dump(isset(self::$_instance)) all returns true. . .
A class worthy of its name has only one instance... I believe everyone should be able to understand this example in depth.

The above is the detailed content of Detailed explanation of singleton pattern in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn