Auto Load Class
Many developers write object-oriented applications, creating a PHP source file for each class definition. A very big
Annoyance is having to write a long list of included files at the beginning of each script (one file per class).
In a software development system, it is not possible to write all the classes in a PHP file, when in a PHP file
When you need to invoke a class declared in another file, you need to introduce the file through include. But sometimes,
In a large number of projects, to each of the required classes of files included in, is a very frustrating thing, so
Can we use any class to import the PHP file where this class is located? This is where we're
The auto-load class to speak.
In PHP5, you can define a __autoload () function that will automatically adjust when you try to use a class that is not already defined
By calling this function, the scripting engine has the last chance to load the required classes before PHP fails.
The __autoload () function receives a parameter that is the class name of the class that you want to load, so when you do the project, the organization defines
The name of the class, you need to follow certain rules, preferably with the class name as the center, or you can add a uniform prefix or suffix shape
into filenames, such as xxx_classname.php, classname_xxx.php, and classname.php, and so on.
This example attempts to load the MyClass1 and MyClass2 classes from the myclass1.php and myclass2.php files, respectively
Code Snippets
Copy the Code code as follows:
function __autoload ($classname) {
Require_once $classname. '. php ';
}
The MyClass1 class does not have an automatic call to the __autoload () function, passing in the parameter "MyClass1"
$obj = new MyClass1 ();
The MyClass2 class does not have an automatic call to the __autoload () function, passing in the parameter "MyClass2"
$obj 2 = new MyClass2 ();
?>
The above describes the object-oriented design principles of PHP object-oriented all-in-one (17) automatic loading class, including the object-oriented design principles of the content, I hope to be interested in PHP tutorial friends helpful.