亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

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

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

Oct 15, 2016 am 10:31 AM

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 {
    
    //第三步:定義一個(gè)變量
    private static $instance= null;

    //第一步:封裝構(gòu)造函數(shù)
    private function  __construct(){
        
    }

    //第二步:使用類名調(diào)用這個(gè)類創(chuàng)建對(duì)象實(shí)例
    static  function getSingle(){
        if( !(self::$instance  instanceof self) ){	//instanceof判斷一個(gè)對(duì)象是否是某個(gè)類的實(shí)例
	    self::$instance = new self();               //用變量來存儲(chǔ)實(shí)例化出來的對(duì)象
	}
	    return self::$instance;
    }


    //第四步:禁止克隆實(shí)例化出來的對(duì)象
    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;   //此處禁止克隆單例對(duì)象實(shí)例
var_dump($obj3);  //Call to private Sing::__clone() from context '' in 錯(cuò)誤行號(hào)


?>

Statement of this Website
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

Hot AI Tools

Undress AI Tool

Undress AI Tool

Undress images for free

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

PHP Tutorial
1488
72