Php _ autoload magic method

Source: Internet
Author: User

Today, however, I found that the _ autoload magic method is too specific or you are willing to call it a magic function. When he loads the class files to be included, he does not even need to declare anything other than the class defined in the class files.

Start to play back this mechanism.

First, create a Test. class. Php file and type the following content:

The code is as follows: Copy code

$ PublicPara = 'When did the 17th CPC National Congress be held? ';
Class Test {
Public function _ construct (){
Global $ publicPara;
If (isset ($ publicPara )){
Echo $ publicPara;
  }
Else {
Echo "what's my problem? ";
  }
 }
}

Note

You have to save this file!

Create a new file named do. php and type the following content:

The code is as follows: Copy code
<? Php
Require_once ('test. class. Php ');
New Test ();
?>

In this case, the output is as expected: When did the 17th CPC National Congress be held?

But when you use the _ autoload magic method, the problem arises.

The code is as follows: Copy code

<? Php
Function _ autoload ($ classname ){
Require_once ($ classname. ". class. php ");
}
New Test ();
?>

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.