Instantiate the class, the first need to introduce class files, but sometimes we do not know that we may need to use those classes, if all the class files are introduced, will cause the waste of resources, this time can be used in Factory mode, specifically for automatic loading, instantiation of the class.
Instance code:
Note: The case assumes that there is a factory folder in which some class files are stored
<?PHP//Create a factory class to batch create objectsclassfactory{//Create a static method Static functionFunc1 ($className){ //Save the file address you need to import to $path $path= ' factory/'.$className.‘. Php; //End If file doesn't exist if(!Is_file($path)) { //echo ' file does not exist '; return false; }Else{ /** * file exists then introduce file * Save class instantiation in file to $obj * return materialized result*/ include $path; $obj=New $className; return $obj; } }}//instantiate an object from a factory$a= Factory::func1 (' A ');Var_dump($a);
PHP_ Factory mode