1, PHP----automatically load class __autoload () function

Source: Internet
Author: User

1 Many developers write object-oriented applications, creating a PHP source file for each class definition. A big annoyance is having to write a long list of included files at the beginning of each script (one file per class). 2 In a software development system, it is not possible to write all the classes in a PHP file, and when you need to invoke a class declared in another file in a PHP file, you need to introduce the file through include. However, sometimes, in a large number of projects, to each of the required classes of files are included in, is a very annoying thing, so we can use what class, and then the class where the PHP file import it? That's what we're going to talk about here. Auto-load class. 3In PHP 5, you can define a __autoload () function that will be called automatically when trying to use a class that has not yet been defined, and by calling this function, the scripting engine has a last chance to load the required class before PHP fails, and a parameter that the __autoload () function receives. is the class name of the class you want to load, so when you do the project, when the organization defines the name of the class, you need to follow certain rules, preferably with the class name as the center, or a uniform prefix or suffix to form the file name, such as xxx_classname.php, classname_ Xxx.php and is classname.PHP and so on. 4This example attempts to separate from myclass1.php and MyClass2.loading MyClass1 and MyClass2 classes in PHP files5016<?PHP7028 function__autoload ($classname)903Ten { One04 A     require_once $classname. '. php '; -05 - } the06 -   -07 - //when the MyClass1 class does not exist, the __autoload () function is called automatically, passing in the parameter "MyClass1" +08 - $obj=NewMyClass1 (); +09 A   at10 - //when the MyClass2 class does not exist, the __autoload () function is called automatically, passing in the parameter "MyClass2" -11 - $obj 2=NewMyClass2 (); -12 -?> inNote: __autoload () is specifically designed for the non-existence of the Class!!! Many frameworks use this function to implement automatic loading of class files!!!

1, PHP----automatically load class __autoload () function

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.