This paper introduces the principle and usage of PHP auto loading class. The details are as follows:
Automatic loading of classes (Autoloading Classes)
When writing OOP programs, many developers create a new PHP file for each class. This brings a worry: at the beginning of each script, you need to include a long list (each class has a file).
In PHP 5, this is no longer necessary. spl_ autoload_ The register() function can register any number of autoloaders and load them automatically when using undefined classes and interfaces. By registering the autoloader, the script engine has a last chance to load the required classes before PHP fails.
Tip
Although__ Autoload() function can also load classes and interfaces automatically, but it is more recommended to use SPL_ autoload_ Register() function. spl_ autoload_ Register () provides a more flexible way to automatically load classes (in the same application, it can support any number of loaders, such as those in third-party libraries). Therefore, it is no longer recommended__ Autoload() function, which may be discarded in later versions.
Note:
Before PHP 5.3__ The exception thrown by autoload function cannot be caught by “catch” statement block and will cause a fatal error. Since PHP 5.3, you can throw custom exceptions, and then you can use custom exception classes__ Autoload function can automatically load custom exception class recursively.
Note:
Automatically load cli interactive modes that are not available for PHP.
Note:
If the class name, for example, is used for “call”_ user_ Func (), it may contain some dangerous characters, such as../。 It is recommended that you do not use user input in such a function, at least in the__ Autoload() to verify the input.
Auto load sample
ClassInterface.php
<?php
interface ClassInterface {
public function print();
public function get_current_time();
}
MyClass1.php
<?php
class MyClass1 implements ClassInterface
{
public $flag = 'MyClass1';
public function print() {
echo $this->flag;
}
public function get_current_time() {
echo $this->flag . " : " . date("Y-m-d H:i:s",time());
}
}
MyClass2.php
<?php
class MyClass2 implements ClassInterface
{
public $flag = 'MyClass2';
public function print() {
echo "MyClass2";
}
public function get_current_time() {
echo $this->flag . " : " . date("Y-m-d H:i:s",time());
}
}
autoload.php
<?php
date_default_timezone_set('Asia/Shanghai');
spl_autoload_register(function ($class_name) {
require_once $class_name . '.php';
});
$obj = new MyClass1();
$obj2 = new MyClass2();
$obj->print();
echo "<br>";
$obj->get_current_time();
echo "<br>";
$obj2->print();
echo "<br>";
$obj2->get_current_time();
Output results:
More about PHP related content, interested readers can see the special topics of this website: “PHP object-oriented programming introductory tutorial”, “PHP array operation skills encyclopedia”, “PHP basic grammar introductory tutorial”, “PHP operation and operator Usage Summary”, “PHP string Usage Summary”, “PHP + MySQL database operation introductory tutorial” and “PHP common database operation” Summary of writing skills
I hope this article is helpful for PHP programming.