Demonstrate factory mode and factory
<? Phpheader ("content-type: text/html; charset = UTF-8"); // demonstrate factory mode/* requirement: design a class. This class has a method, this effect can be achieved: If a class name is input to the method, the method can return the object of this class. It is like a "Plastic Product" Factory: To give it a "Mold" of a certain product, you can produce the plastic products corresponding to the mold. * // Factory class Factory {static function GetObjectByClassName ($ class_name) {$ obj1 = new $ class_name (); return $ obj1 ;}} class {}; class B {}; class C {}; // $ F = new Factory (); // $ obj1 = $ F-> GetObjectByClassName (""); $ obj1 = Factory: GetObjectByClassName ("A"); $ obj2 = Factory: GetObjectByClassName ("A"); $ obj3 = Factory: GetObjectByClassName ("B "); $ obj4 = Factory: GetObjectByClassName ("C"); $ obj5 = Factory: GetObjectByClassName ("A"); echo "<br/>"; var_dump ($ obj1 ); echo "<br/>"; var_dump ($ obj2); echo "<br/>"; var_dump ($ obj3); echo "<br/> "; var_dump ($ obj4); echo "<br/>"; var_dump ($ obj5);/* object (A) #2 (0) {} object () #3 (0) {} object (B) #4 (0) {} object (C) #5 (0) {} object (A) #6 (0) {}*/