This article will give you a brief introduction to the php automatic loading class__ autoload () method. I hope this method will be helpful to you.
Purpose: When a class needs to be instantiated in the program, but this class is not in this file, you need to use the include function to include the external file. However, when there are many external classes to use, it will be very complicated to use the include function. This is the option to use the _ autoload () global function to automatically load the class.
When in index. to use the first three classes in php, you need to write three include ("name. class. php ") This method is very inefficient, but if you use the _ autoload () function, you don't have to worry about it. You just need to write this function method:
In PHP 5, this is no longer needed. You can define a _ autoload () function, which is automatically called when trying to use a class that has not been defined. By calling this function, the script engine has the last chance to load the required class before a PHP error fails.
In the following example, we will use an instance to explain how _ autoload () is used.
First, define a class ClassA named ClassA. class. php.
The Code is as follows: |
Copy code |
Class ClassA { Public function funa (){ Echo "classA loaded successfully! <Br> "; } } |
Then, define another class ClassB in the same directory. The file name is ClassB. class. php, and ClassB inherits ClassA.
<? Php
The Code is as follows: |
Copy code |
Class ClassB extends ClassA { Public function funb (){ Echo "classB also loaded successfully! <Br> "; } } |
Finally, define an autoload. php file in the same directory as the above two files (this file name can be obtained randomly)
The Code is as follows: |
Copy code |
<? Php Function _ autoload ($ class_name ){ Require_once ("./". ucfirst ($ class_name). '. class. php'); // ucfirst converts the first letter of a class name to uppercase. } $ Obj = new ClassB (); $ Obj-> funa (); $ Obj-> funb (); ?> |
Run the autoload. php program and check the result:
ClassA loaded successfully!
ClassB also loaded successfully!
Example: The folder contains the following files:
User. class. php;
Person. class. php;
Message. class. php;
Index. php;
We can do this.
The Code is as follows: |
Copy code |
Index. php Function _ autoload ($ className) {// ucfirst () converts the first letter of the string to uppercase. Include ("ucfirst ($ className)". class. ". php ");} /************** |
* For example, if the User class in User. class. php is instantiated in the index. php file, the _ autoload () function is automatically called if it does not exist.
The Code is as follows: |
Copy code |
* Pass the class name User as a parameter *************/ $ User = new User (); // Call the User. class. php file by automatically loading the class $ Person = new Person (); // Call the Person. class. php file by automatically loading the class $ Message = new Message (); // Call the Message. classphp file function = "" message = "new" person = "new" user = "new"> |