Implementation of encapsulating singleton pattern with trail in PHP

Time:2021-11-24

Definition of singleton mode

Ensure that a class has only one instance, cannot duplicate instances, can only instantiate itself, and provide this instance to the whole system.

Problems solved

That is, a class is instantiated only once. When others instantiate it again, the first instantiated object is returned, which can avoid a large number of new operations and reduce resource consumption.

principle:Four steps, three private and one public

  • Privatize static properties
  • Privatization construction method
  • Privatized cloning method
  • Public static method

Packaging background

When there are multiple singleton classes in the project, each new class has to be defined as three private and one public. It seems that some repetitive work is done, which not only wastes time and energy, but also the code is bloated and difficult to maintain. Is there a more elegant way to write it?

Trail to encapsulate the singleton

PHP has always been a single inheritance language from the past to the present. It cannot inherit properties and methods from two base classes at the same time. In order to solve this problem, PHP has the feature of trail
Usage: declare the trail name to be combined by using the use keyword in the class. The specific trail declaration uses the trail keyword. Trail cannot be instantiated.

Code example

Create a trail

<?php 
/** 
 *Trail singleton singleton mode 
 */ 
 
Trait Singleton 
{ 
 
 private static $instance = null; 
 
 private function __construct() 
 { 
  parent::__construct(); 
  //Privatization construction method 
 } 
 
 private function __clone() 
 { 
  //Privatized cloning method 
 } 
 
 public function __sleep() 
 { 
  //Rewrite__ Sleep method, which will return null to prevent serialization and deserialization from obtaining new objects 
  return []; 
 } 
 
 public static function getInstance() 
 { 
  if (!isset(self::$instance)) { 
    self::$instance = new static();// There is no difference between new self(), self and static 
  } 
  return self::$instance; 
 } 
}

Multi inheritance usage

Other classes are only used in the code. Use Singleton, You can inherit the singleton

<?php 
/** 
 *Desc: inherited instance of business class 1
 */ 
 
class YieWu1
{ 
 use Singleton; //  Key line of code
 
 public function getInfo(){
   //Business code
 }
}
<?php 
/** 
 *Desc: business class 2 inheritance instance
 */ 
 
class YieWu2
{ 
 use Singleton; //  Key line of code
 
 public function getInfo(){
   //Business code
 }
}

Real column call


YieWu1::getInstance()->getInfo();
YieWu2::getInstance()->getInfo();

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support developpaer.

Recommended Today

Apache sqoop

Source: dark horse big data 1.png From the standpoint of Apache, data flow can be divided into data import and export: Import: data import. RDBMS—–>Hadoop Export: data export. Hadoop—->RDBMS 1.2 sqoop installation The prerequisite for installing sqoop is that you already have a Java and Hadoop environment. Latest stable version: 1.4.6 Download the sqoop installation […]