Magic method_autoload () instance details (php object oriented advanced tutorial)

Source: Internet
Author: User
When writing code, you often encounter a headache, that is, to introduce a lot of classes in a page, you need to use the include_once or require_once () function to introduce them one by one. It is acceptable when there are not many introduced content, but if there are more than a dozen or dozens of files that need to be introduced, there are many operations, so don't bother to say, there will also be repeated introductions or missing references. When writing code, you often encounter a headache, that is, to introduce a lot of classes in a page, you need to use the include_once or require_once () function to introduce them one by one. It is acceptable when there are not many introduced content, but if there are more than a dozen or dozens of files that need to be introduced, there are many operations, so don't bother to say, there will also be repeated introductions or missing references.

In PHP5, we can use the _ autoload () method to solve this problem. The __autoload () method can automatically instantiate the class to be used. When the program uses a class but the class has not yet been instantiated, PHP5 will call the _ autoload () method to automatically search for files with the same name as the class in the specified path. If it is found, the program continues; otherwise, an error is reported.

Note:

All other methods work only when they are added inside the class. __autoload () is the only method not added in the class.

As long as a class is used on the page, the class name is automatically passed to this parameter.

For example:

The code of the class file sport. class. php:

 type = $type;}public function __toString(){return $this->type;}}?>

Code in the index. php file:

 

First, do not instantiate the common classes in the class file sport. class. php. Then introduce the class in the class file in the index. php file of php.

The above is the details of the magic method _ autoload () instance (php object oriented advanced tutorial). For more information, see other related articles in the first PHP community!

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.