/** 2.singleton*/class singleton{private static $instance = null;public static function getinstance () {if (!isset (self::$ Instance) { $c = __class__; Self:: $instance = new $c; } Return self:: $instance;} Public Function Eventresult ($id) {return $id;} protected function __construct () { } Private Function __clone () { } Private Function __ Wakeup () { }}class singletonchild extends singleton{} $obj = Singleton::getinstance (); Var_dump ($obj = = = Singleton::getinstance ()); $ANOTHEROBJ = Singletonchild::getinstance (); Var_dump ($anotherObj = = = Singleton::getinstance ()); Var_dump ($ANOTHEROBJ = = = Singletonchild::getinstance ()); $objSingle = Singleton::getinstance (); $result = $objSingle->eventresult;p rint_r ($result);
Note that for: __construct,__clone,__wakeup modifier settings prevent the singleton from being broken by the instance.
[PHP Learning] Singleton mode