In-depth explanation of the PHPautoload mechanism-PHP Tutorial

Source: Internet
Author: User
Go deep into the PHPautoload mechanism. When using the php oo mode to develop a system, we usually get used to storing the implementation of each class in a separate file, which makes it easy to reuse classes, at the same time, in the future, when we use PHP's OO mode to develop a system, we usually get used to storing the implementation of each class in a separate file, which makes it easy to reuse classes, at the same time, it will be very convenient for future maintenance. This is also one of the basic ideas of OO design. To use a class before PHP5, you only need to use Include/requireInclude it.
The following is an example:

The code is as follows:


/* Person. class. php */
Class Person {
Var $ name, $ age;

Function _ construct ($ name, $ age)
{
$ This-> name = $ name;
$ This-> age = $ age;
}
}
?>

/* No_autoload.php */
Require_once ("Person. class. php ");

$ Person = new Person ("Altair", 6 );
Var_dump ($ person );
?>


In this example, the no-autoload.php file needs to use the Person class, which uses require_once to include it, and then you can directly use the Person class to instantiate an object.

However, as the project scale continues to expand, using this method will bring about some implicit problems: if a php file needs to use many other classes, a lot of require/include statements are required, this may cause omissions or include unnecessary class files. If a large number of files require other classes, it is a nightmare to ensure that each file contains the correct class files.

PHP5 provides a solution for this problem, which is the automatic loading mechanism of the class. The autoload mechanism makes it possible for PHP programs to automatically include class files when using classes, rather than include all class files at the beginning. this mechanism is also called lazy loading.

The following is an example of using the autoload mechanism to load the Person class:

The code is as follows:


/* Autoload. php */
Function _ autoload ($ classname ){
Require_once ($ classname. "class. php ");
}

$ Person = new Person ("Altair", 6 );
Var_dump ($ person );
?>


...

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.