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