One of the main application scenarios of PHP is the application scenario where the application deals with the database. Therefore, there will be a large number of database operations in an application, such as the behavior of connecting to the database through the database handle. Using the singleton mode can avoid a large number of new operation, because each new operation consumes memory resources and system resources.
[Advantages of singleton mode]
1. Controlled access to unique instance
2. Reduce the namespace. The singleton mode is an improvement on global variables. It avoids polluting the namespace with global variables that store unique instances
3. Allows for the essence of operation and presentation. Singleton classes can have subclasses. And it is very easy to configure an application with an instance of this extension class. You can configure your application at runtime with instances of the classes you need.
4. Allow a variable number of instances (multiple instance mode)
5. More flexible than class operations
[Applicable scenarios for singleton mode]
1. When a class can only have one instance and clients can access it from a well-known access point
2. When this only instance should be extensible through subclassing. And users should be able to use an extended instance without changing their code.
【Single case mode and other modes】
Factory method pattern: The singleton pattern uses the factory pattern to provide its own instances.
Abstract factory pattern (abstract factory pattern): The abstract factory pattern can use the singleton pattern to design the specific factory class into a singleton class.
Builder mode (Builder mode): The construction mode can design specific construction classes into singleton mode
3. Singleton pattern instance
3.1 test.php
SPAN style="FONT-SIZE: 14px"><?php class Test{ public $service; //静态成品变量 保存全局实例 private static $instance = null; //用mysqli连接数据库 private static $SDB_USER = "test"; private static $SDB_DBNAME="test"; private static $SDB_HOST = 'localhost'; private static $SDB_PASS = 'ddddYQ3ddddUxEY'; //私有化构造函数,防止外界实例化对象 private function __construct() { $this->service = new mysqli(self::$SDB_HOST, self::$SDB_USER, self::$SDB_PASS, self::$SDB_DBNAME); } //私有化克隆函数,防止外界克隆对象 private function __clone(){ } //静态方法, 单例统一访问入口 public static function getInstance() { if (!isset(self::$instance) || is_null(self::$instance)) { self::$instance = new Test(); } return self::$instance; } //测试方法: 打印hello,world public function sayHello() { echo 'hello,world'; } } </SPAN> <?php class Test{ public $service; //静态成品变量 保存全局实例 private static $instance = null; //用mysqli连接数据库 private static $SDB_USER = "test"; private static $SDB_DBNAME="test"; private static $SDB_HOST = 'localhost'; private static $SDB_PASS = 'ddddYQ3ddddUxEY'; //私有化构造函数,防止外界实例化对象 private function __construct() { $this->service = new mysqli(self::$SDB_HOST, self::$SDB_USER, self::$SDB_PASS, self::$SDB_DBNAME); } //私有化克隆函数,防止外界克隆对象 private function __clone(){ } //静态方法, 单例统一访问入口 public static function getInstance() { if (!isset(self::$instance) || is_null(self::$instance)) { self::$instance = new Test(); } return self::$instance; } //测试方法: 打印hello,world public function sayHello() { echo 'hello,world'; } }
3.2 data.php
SPAN style="FONT-SIZE: 14px"><?php require_once('test.php'); class TestObject { protected $test; function __construct($id=null) { $this->test = Test::getInstance(); $this->test->sayHello(); } } $obj = new TestObject();</SPAN> <?php require_once('test.php'); class TestObject { protected $test; function __construct($id=null) { $this->test = Test::getInstance(); $this->test->sayHello(); } }
$obj = new TestObject();
Run data.php, result:
4.The core points of PHP singleton mode implementation are as follows:
1. You need a static member variable that holds the only instance of the class (usually the $instance private variable)
2. The constructor and clone function must be declared private. This is to prevent external programs from adding new classes and losing the meaning of the singleton mode
3. You must provide a public static method (usually the getInstance method) to access this instance, thereby returning a reference to the unique instance
5.Disadvantages of PHP singleton mode
As we all know, the PHP language is an interpreted scripting language. This operating mechanism allows all related resources to be recycled after each PHP page is interpreted and executed. In other words, PHP has no way to make an object resident in memory at the language level. This is different from compiled types such as asp.net and Java. For example, in Java, a singleton will always exist throughout the life cycle of the application. Variables are cross-page level and can truly make this instance unique in the application life cycle. However, in PHP, all variables, whether they are global variables or static members of the class, are page-level. Every time the page is executed, a new object will be re-established and will be cleared after the page is executed. It seems that PHP The singleton mode is meaningless, so I think the PHP singleton mode is very meaningful only when multiple application scenarios occur in a single page-level request and need to share the same object resource.