PHP Magic Method __autoload ()

Source: Internet
Author: User

PHP before the Magic Function __autoload () method appears, if you want to instantiate 100 objects in a program file, then you have to include 100 class files with include or require, or you have to define the 100 classes in the same class file-- I believe this file will be very big.

But the __autoload () method comes out, and you don't have to worry about it later, and this class automatically loads the files you've created before you instantiate the object.

Let's take a look at an example of how to use it, and later explain how to make

 php  /*  * * Description of ClassA * * @author Timeless  */ class   ClassA { // put your code here  
   
    public  
     function __construct () {echo  
     "
     classa load success<br/>   Span style= "color: #000000;"    >; }}
   
classb.php file
<? PHP /* */class ClassB {//put your codehere public function __construct () { 'ClassB load success<br/>' ; }}
index.php

<?PHP/** * @author Zhaoxing <[email protected]> * @package phpclass * @param string $classname class name * Php Magic Method __autoload (); */function __autoload ($classname) {$classpath='.'. Directory_separator. $classname.'. PHP'; if(File_exists ($classpath)) {require_once $classpath; } Else{echo'class file'. $classpath.'Not found'; }} $newClassA=NewClassA (); $newClassB=NewClassB ();
Results:
ClassA Load SUCCESSCLASSB Load success

PHP Magic Method __autoload ()

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.