//PHP design mode//Factory mode: Factory class for creating objectsInterfacedongwu{function Say ();}classMao implements dongwu{ Publicfunction Say () {echo"I'm a little wildcat!"; }}//factory class, used to create cat classclassgongchang{ Public Staticfunction Getobj ($className) {return New$className (); }} $mao= Gongchang::getobj ("Mao"); $mao->say ();
<?PHP//PHP design mode//single-case modeclassmao{Private Static$_getobj;//Static Members Privatefunction __construct () {}//The constructor is preceded by a private, and the class cannot be instantiated Publicfunction Say () {echo"Meow Meow"; } //create an instance of the class yourself Public Staticfunction Getobj () {if(self::$_getobj==NULL)//assign a value only once{self::$_getobj=NewMao (); } returnSelf::$_getobj;//returning examples of cat classes}} $mao=mao::getobj (); $mao->say ();
PHP Basic---Singleton mode && Factory mode