Home  >  Article  >  php教程  >  Singleton mode is commonly known as singleton 3 steps + 1 song

Singleton mode is commonly known as singleton 3 steps + 1 song

WBOY
WBOYOriginal
2016-10-15 10:31:481433browse

What is singleton pattern?
This design pattern allows only one object instance to be instantiated through this class in the entire application

Pattern classification?

In all pattern designs, there are three basic design patterns, singleton pattern, factory pattern, and registration tree pattern. Other patterns are often based on these patterns. Today we bring the singleton pattern.


Why use singleton pattern?
1. PHP often deals with databases. If connection objects are frequently established and new operations are performed in the application, a large amount of system memory resources will be consumed. (Save resource overhead)
2. In team cooperation projects, the singleton mode can effectively avoid artificial system consumption caused by different programmers new their own objects. (Save resource overhead)


------------------Achieving 3 steps + 1 song for a single instance ------------------

Step 1: Encapsulate the constructor method private __construct( ) { }
Reason: The constructor is the first method called when new creates an object. The constructor is declared as private or protected, which is destined to fail through new. Method creates instance objects.

Step 2: Create object instances through methods within the class. static Single(){ }
Reason: We often call the object's method after creating the object, and at this time we need to call the method in the class to create the object. The solution to a method that can be called regardless of whether the object is created or not is undoubtedly to use the keyword --static
    ​​​​​​​​​​​​​​​​​​​

Step 3: Define an encapsulated static variable private static $instance

Reason: Put the only instantiated object in this variable and store it
Step 4 (plus 1 song): Private The magic method of cloning: __c lo n e ();
Reason: For an object of a class, if you use the "clone operator", a new object that is exactly the same as the current object will be cloned, and: at this time this The new object will also automatically call the magic method in the class: _ _c l o n e (); as long as there is this method;


Code Demo

<?php

class Sing {
    
    //第三步:定义一个变量
    private static $instance= null;

    //第一步:封装构造函数
    private function  __construct(){
        
    }

    //第二步:使用类名调用这个类创建对象实例
    static  function getSingle(){
        if( !(self::$instance  instanceof self) ){	//instanceof判断一个对象是否是某个类的实例
	    self::$instance = new self();               //用变量来存储实例化出来的对象
	}
	    return self::$instance;
    }


    //第四步:禁止克隆实例化出来的对象
    private function __clone(){ }

}

 $danli = Sing::getSingle();
 var_dump($danli);    //输出   object(Sing)#1 (0) { }
 $danli2 = Sing::getSingle();   
 var_dump($danli2);   //输出   object(Sing)#1 (0) { }

$obj3 = clone $danli;   //此处禁止克隆单例对象实例
var_dump($obj3);  //Call to private Sing::__clone() from context '' in 错误行号


?>

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
Previous article:CP936 converted to UTF-8Next article:CP936 converted to UTF-8