Home >Backend Development >PHP Tutorial >How to use the Plants vs. Zombies modifier How to use the PHP __autoload function (automatically load class files)
This is also one of the basic ideas of OO design. Before PHP5, if you need to use a class, you only need to include it directly using include/require. Here is a practical example:
Copy code The code is as follows:
class ClassA{
public function __construct(){
echo “ClassA load success!”;
}
}
//Define a class ClassA, the file name is ClassA.php
class ClassA{
public function __construct(){
echo “ClassA load success!”;
}
}
class ClassB extends ClassA {
public function __construct(){
//parent: :__construct();
echo “ClassB load success!”;
}
}
//Define a class ClassB, the file name is ClassB.php, ClassB inherits ClassA
class ClassB extends ClassA {
public function __construct(){
//parent::__construct();
echo “ClassB load success!”;
}
}
After defining two classes for testing, let’s write a PHP running program file containing the __autoload() method as follows:
function __autoload($classname){
$classpath=”./”.$classname.'.php';
if(file_exists($classpath)){
require_once($classpath);
}
else{
echo 'class file'.$classpath.'not found!';
}
}
$newobj = new ClassA();
$newobj = new ClassB();
The above introduces how to use the Plants vs. Zombies modifier and how to use the PHP __autoload function (automatic loading of class files), including how to use the Plants vs. Zombies modifier. I hope it will be helpful to friends who are interested in PHP tutorials.