Section 12th -- Automatic loading of classes -- ClassesandObjectsinPHP5 [12] _ PHP Tutorial

Source: Internet
Author: User
Section 12th -- Automatic loading of classes -- ClassesandObjectsinPHP5 [12]. Section 12th -- Automatic loading of classes when you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all Section 12th -- Automatic loading of classes

When you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all, you know which class to use. however, PHP provides the automatic class loading function, which can save programming time. when you try to use a class that is not organized by PHP, it will look for a global function of _ autoload. if this function exists, PHP will use a parameter to call it. the parameter is the name of the class.

Example 6.15 illustrates how _ autoload is used. it assumes that each file in the current directory corresponds to a class. when the script tries to generate a User-like instance, PHP will execute _ autoload. the script assumes that class_User.php defines the User class .. PHP returns the lower-case name no matter whether it is in upper or lower case.

Listing 6.15 Class autoloading
 name = "Leon";    $u->printName(); ?> 

Lifecycle 12th-automatic loading of classes when you try to use an undefined class, PHP will report a fatal error. the solution is to add a class and include a file. after all...

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.