The example of a single example of the PHP design pattern, for your reference, the details are as follows
<?php/** * PHP design mode single case mode/class fruit{private static $instanceMap = Array (); Protected getter for singleton instances protected static function Getsingleton ($className) {//Guaranteed single case mode and not from controller
Instantiate and Clone if (!isset (self:: $instanceMap [$className])) {$object = new $className; Make sure this object inherit from Singleton if ($object instanceof Fruit) {self:: $instanceMap [$class
Name] = $object;
Var_dump ($object);
else {throw singletonexception ("Class ' $className ' does not inherit from singleton!");
} return Self:: $instanceMap [$className]; }//protected constructor to prevent outside instantiation protected function __construct () {}//denie Clonin G of Singleton objects public final function __clone () {trigger_error (' It's impossible to clone Singleton ', E_u
SER_ERROR);
Class Apple extends fruit{protected $rndId; Public funCtion __construct () {$this->rndid = rand (); The Public Function Whatami () {echo ' I am-A-Apple ('. $this->rndid.
') <br/> ';
public static function getinstance () {//echo get_class ();
Return Fruit::getsingleton (Get_class ()); Class Greenapple extends apple{public Function Whatami () {echo ' I am a greenapple ('. $this->rndid .
') <br/> ';
The public static function, getinstance () {return Fruit::getsingleton (Get_class ());
}} $apple 1 = apple::getinstance ();
Var_dump ($apple 1);
$apple 2 = greenapple::getinstance (); $apple 1->whatami ();//should Echo ' I am a Apple (some number) $apple 2->whatami ()//should echo ' I am a greenapple (
Some number) $apple 1 = apple::getinstance ();
$apple 2 = greenapple::getinstance (); Guaranteed single case mode $apple 1->whatami ()//should Echo ' I am a Apple (same number as above) $apple 2->whatami ();//should Echo ' I am a greenapple (same number as above)
$a = Clone $apple 1;//This should fail//$b = Clone $apple 2;//This should fail
The above is the entire content of this article, I hope that you learn PHP program help.