Home > Backend Development > PHP Tutorial > Detailed explanation of __autoload() method in php_PHP tutorial

Detailed explanation of __autoload() method in php_PHP tutorial

WBOY
Release: 2016-07-20 11:10:30
Original
794 people have browsed it

Before the magic function __autoload() method appeared in PHP, if you wanted to instantiate 100 objects in a program file, then you had to include 100 class files using include or require, or you defined these 100 classes in the same program file. In a class file - I believe this file will be very large.

But with the __autoload() method, you don’t have to worry about it in the future. This class will automatically load the specified file before you instantiate the object.

Let’s take a look at the specific usage through an example, and explain later what you should pay attention to when using the PHP magic function __autoload.

After defining two classes for testing, we To write a PHP running program file containing the __autoload() method as follows:
The code is as follows
 代码如下 复制代码

 //定义一个类ClassA,文件名为ClassA.php
class ClassA{
 public  function __construct(){
  echo "ClassA load success!";
 }
}

 //定义一个类ClassB,文件名为ClassB.php,ClassB继承ClassA
class ClassB extends ClassA {
 public function __construct(){
  //parent::__construct();
  echo "ClassB load success!";
 }
}

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();

//Define a class ClassA, the file name is ClassA.php

class ClassA{
public function __construct(){

echo "ClassA load success!";

}
}

 代码如下 复制代码
Fatal error: Class ‘Classd’ not found in ……ClassB.php on line 2,
//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){ if(file_exists($classpath)){ require_once($classpath); } else{ echo 'class file'.$classpath.'not found!'; }} $newobj = new ClassA();$newobj = new ClassB();
The operation of this file is There is no problem at all, which shows how easy to use autoload is, haha...
But I have to remind you that you must pay attention to several aspects. 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 you use the __autoload magic function to instantiate ClassB, you will receive a fatal error: 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 file name of the class must be consistent to make it easier to use the magic function __autoload; Other things to note: 3. Run PHP in CLI mode This method is invalid for scripts; 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: .././ like this The file name is very dangerous. http://www.bkjia.com/PHPjc/444710.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/444710.htmlTechArticlePHP Before the magic function __autoload() method appeared, if you wanted to instantiate 100 in a program file Object, then you must use include or require to include 100 class files, or...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template