Home>Article>Backend Development> Explanation of PHP singleton mode (code example)

Explanation of PHP singleton mode (code example)

不言
不言 forward
2018-10-24 17:30:03 2820browse

This article brings you an explanation (code example) about the PHP singleton mode. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The singleton pattern is a commonly used design pattern and can be seen in many frameworks. The singleton mode can ensure that there is only one instance of a class, thereby facilitating control of the number of instances and saving system resources.


      

The singleton pattern may be used multiple times in a system. For easier creation, you can try to establish a general abstraction:

// SingletonFacotry.php 
      
// A.php num++; var_dump($obj1->num); // 1 $obj2 = A::getInstance(); $obj2->num++; var_dump($obj2->num); // 2

The above is the detailed content of Explanation of PHP singleton mode (code example). For more information, please follow other related articles on the PHP Chinese website!

php
Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete