Basic PHP knowledge: class and object (2) Automatic Object Loading

Source: Internet
Author: User
Tags autoload

Automatically load objects:
Many developers write object-oriented applications ProgramCreate a PHP source file for each class definition. A major headache is that you have to write a long list of contained files at the beginning of each script (each class has one file.
In PHP 5, this is no longer needed. You can define a _ autoload function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.

In this example, we try to load the myclass1 and myclass2 classes from the myclass1.php and myclass2.php files respectively.
Function _ autoload ($ class_name ){
Require_once $ class_name. '. php ';
}
$ OBJ = new myclass1 ();
$ Obj2 = new myclass2 ();
Note:
The exception thrown in the _ autoload function cannot be caught by the catch statement block and cause a fatal error.

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.