This article mainly for you to introduce the PHP singleton model and factory model of the relevant information, with a certain reference value, interested in small partners can refer to
One, the singleton mode is also called the responsibility mode, it is used in the program to create a single function of the access point, in layman's name is the object is unique.
All singleton modes have at least the following three common elements:
1. They must have a constructor and must be marked as private
2. They have a static member variable that holds an instance of the class
3. They have a public static method that accesses this instance
A singleton class cannot be instantiated directly in any other class, but only by its own instantiation. Instead of creating an instance copy, it returns a reference to the instance stored inside the singleton class.
Singleton Pattern Example
<?phpclass Single { private $name;//Declare a private instance variable private function __construct () {// Declares a private construction method in order to prevent external code from using new to create an object. } static public $instance;//declares a static variable (a unique instance saved in the Class) static public function getinstance () {//Declares a getinstance () static method, Used to detect if there is an instance object if (!self:: $instance) Self:: $instance = new self (); Return self:: $instance; } Public Function SetName ($n) {//assigns a value to variable $name $this->name = $n; } public function GetName () {//takes the value of the variable $name return $this->name; }} $a = Single::getinstance (); $b = Single::getinstance (); $a->setname (' Hello World '); $b->setname (' Good Morning '); echo $a->getname ();//good morning echo $b->getname ();//good morning?>
Factory mode is a class that has some methods for creating objects for you, so you can use the factory class to create objects instead of using new directly.
This way, if you want to change the type of object you create, you can simply change the factory.
<?php//Factory Class (Operations on Operators) class factory{public static function dx ($ope) { switch ($ope) {case "+": return new Plus (); break; Case "-": return new sub (); break; Case "%": return new REM (); break; } }} $ope = Factory::d x ("+"), $ope->a =; $ope->b = 10;echo $ope->opera ();
Factory mode Instances
Class Factory {//Create a basic factory class static public function FAC ($id) {//Create a static method that returns an instance of an object if (1 = = $id) return new A (); ElseIf (2== $id) return new B (); ElseIf (3== $id) return new C (); return new D (); } } Interface Fetchname {//Create an interface public function getname ();/} class A implements fetchname{ private $ Name = "AAAAA"; Public Function GetName () {return $this->name; }} class C implements fetchname{ private $name = "CCCCC"; Public Function GetName () {return $this->name; }} class B implements fetchname{ private $name = "bbbbb"; Public Function GetName () {return $this->name; }} class D implements fetchname{ private $name = "ddddd"; Public Function GetName () {return $this->name; }} $o = FACTORY::FAC (6);//Call the method in the factory class if ($o instanceof fetchname) {echo $o->getname ();//ddddd} $p =factory: : FAC (3); echo $p->getname ();//CCCCC? >