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 ()