PHP_ Factory mode

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.