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. 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
// Define autoload function
Function _ autoload ($ class)
{
Include ("class _". ucfirst ($ class). ". php ");
}
// Use a class that must be autoloaded
$ U = new User;
$ U-> name = "Leon ";
$ U-> printName ();
?>
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.