Sql_autoload_register () function

Source: Internet
Author: User

When reviewing __autoload, I saw the function of Spl_autoload_register (). But suddenly did not understand, by looking at the information I sort of understand.

1.__autoload ()--Auto Load function

I put a lot of class files in the class directory in the form of class.php, creating a index.php in class's sibling directory. Enter the class directory to create Class1.class.php, class2.class.php, class3.class.php files, respectively, for the files inside add code

<?php//in class1.class.phpclassclass1{ Public function__construct () {Echo"Class1"; }}?> <?php//in class2.class.phpclassclass2{ Public function__construct () {Echo"Class2"; }}?> <?php//in class3.class.phpclassclass3{ Public function__construct () {Echo"Class3"; }}?>

Write in index.php file

1<?phpfunction__autoload ($classname){2     $filename= "./class/".$classname.". Class.php ";3     if(Is_file($filename)){4         include $filename;5     }6 }7  $test 1=NewClass1 ();Echo' <br/> ';$test 1=NewClass2 ();Echo' <br/> ';$test 1=NewClass3 ();8  //The result is Class1 .9 Class2Ten CLASS3 One    A?>

We succeeded in automatically loading all the classes underneath the class to be loaded.

2.spl_autoload_register ()--register __autoload () function

No changes are made to the files in class, just a simple rewrite of the index.php

1<?php//Write a loadclass function2 the//LoadClass function does not have the ability to load classes automatically3 functionLoadClass ($classname){4     $filename= "./class/".$classname.". Class.php ";5     if(Is_file($filename)){6         include $filename;7     }8}//The spl_autoload_register () function allows this loadclass to have the function of the automatic loading class Spl_autoload_register ("LoadClass");9  $test 1=NewClass1 ();Echo' <br/> ';$test 1=NewClass2 ();Echo' <br/> ';$test 1=NewClass3 ();Ten?>

Sql_autoload_register () function

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.