Usage and some problems of PHP magic function __autoload_PHP tutorial

WBOY
Release: 2016-07-13 10:56:27
Original
922 people have browsed it

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
 代码如下 复制代码

function __autoload($class_name) {
require_once $class_name . '.php';
}

$obj = new MyClass1();
$obj2 = new MyClass2();
?>

function __autoload($class_name) {
​ require_once $class_name . '.php';

}

$obj = new MyClass1();
代码如下 复制代码


//定义一个类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!";
}
}

$obj2 = new MyClass2();

?>

 代码如下 复制代码

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

Before the magic function __autoload() method appeared in PHP, if you wanted to instantiate 100 objects in a program file, then you must use include or require to include 100 class files, or you must include these 100 classes Defined in the same 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.
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!"; } }
After defining two classes for testing, let’s write a PHP running program file containing the __autoload() method as follows:
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
 代码如下 复制代码
class Test{
 public function  __construct(){
  global $publicPara;
  if(isset($publicPara)){
   echo $publicPara;
  }
  else{
   echo "管我啥事儿了?";
  }
 }
}
Copy code

class Test{

public function __construct(){

global $publicPara;
 代码如下 复制代码
  require_once('Test.class.php');
new Test();
if(isset($publicPara)){

echo $publicPara;

}

else{

echo "What do you care about me?";
 代码如下 复制代码
  function __autoload($classname){
require_once($classname.".class.php");
}
new Test();
}

}

}

Remember to save this file!

Then create a new file named do.php and type the following content

The code is as follows Copy code
new Test(); In this case, the output is just as we expected: When will the 17th National Congress of the Communist Party of China be held? But when you use the magic method __autoload, problems arise
The code is as follows Copy code
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!