This article will talk about a new function of php5. We will introduce the usage and some problems of PHP magic function __autoload. Here is a summary of some problems and precautions that arise during the usage.
__autoload() usage
Some teachings in the php manual
Auto-loading objects
Many developers writing object-oriented applications create a PHP source file for each class definition. A big annoyance is having to write a long list of include files at the beginning of each script (one file per class).
In PHP 5, this is no longer necessary. You can define an __autoload function that will be automatically called when trying to use a class that has not yet been defined. By calling this function, the scripting engine has a last chance to load the required classes before PHP fails with an error.
Note:
Exceptions thrown in the __autoload function cannot be caught by the catch statement block and result in a fatal error.
Note:
Autoloading does not exist when using PHP's CLI interactive mode.
Example #1 Autoload example
This example attempts to load the MyClass1 and MyClass2 classes from the MyClass1.php and MyClass2.php files respectively.
The code is as follows | Copy code | ||||||||
require_once $class_name . '.php'; } $obj = new MyClass1();
?> |
代码如下 | 复制代码 |
function __autoload($classname){ |
The code is as follows | Copy code |
//Define a class ClassA, the file name is ClassA.php class ClassA{ public function __construct(){ echo "ClassA 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!"; } } |
The code is as follows | Copy code |
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(); |
There is no problem running this file, which shows how easy to use autoload is, haha...
But I have to remind you that there are several aspects that you must pay attention to.
1. If the class has an inheritance relationship (for example: ClassB extends ClassA), and ClassA is not in the directory where ClassB is located
When using the __autoload magic function to instantiate ClassB, you will receive a fatal error:
Fatal error: Class ‘Classd’ not found in ……ClassB.php on line 2,
Solution: Put all classes with extends relationship in the same file directory, or manually include the inherited class in the file when instantiating an inherited class;
2. Another thing to note is that the class name and the class file name must be consistent to make it easier to use the magic function __autoload;
Other things to note:
3. This method is invalid when running PHP scripts in CLI mode;
4. If your class name is related to user input - or depends on user input, be sure to check the input file name, for example: .././ Such file names are very dangerous.
Problem with __autoload
__autoload magic method or you want to call it magic function, it is too specific. When he loads the class file that needs to be included, it does not even care about other statements in the class file other than the class definition.
Start replaying this mechanic.
First we create a Test.class.php file and type the following content
$publicPara='When will the 17th National Congress of the Communist Party of China be held? ';
The code is as follows
|
Copy code
|
||||||||
class Test{ public function __construct(){ global $publicPara;
echo $publicPara; }else{ echo "What do you care about me?";
} }
|
Then create a new file named do.php and type the following content