Home > Backend Development > PHP Tutorial > PHP automatically loads class __autoload() method_PHP tutorial

PHP automatically loads class __autoload() method_PHP tutorial

WBOY
Release: 2016-07-13 10:49:41
Original
865 people have browsed it

This article will give you a brief introduction to the __autoload() method of automatically loading classes in PHP. I hope this method will be helpful to all students.

Function: When you need to instantiate a class in the program, and 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 be used, you will find that using included functions will be very cumbersome. In this case, you can use the __autoload() global function to automatically load classes.

When you want to use the first three classes in index.php, you need to write three methods such as include("name.class.php"), which will be very inefficient, but if you use __autoload() Functions don’t have to be so troublesome. You only need to write a function method like this:

In PHP 5, this is no longer necessary. You can define an __autoload() function that is 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.

The following example will illustrate how __autoload() is used.


First define a class ClassA, the file name is ClassA.class.php

The code is as follows Copy code
 代码如下 复制代码

 class ClassA{ 

 public  function funa(){ 

   echo "classA  loaded successfully!
"; 

  } 

}

class ClassA{

public function funa(){

echo "classA loaded successfully!
";
 代码如下 复制代码

 class ClassB extends ClassA { 

 public function funb(){ 

                                                       

   echo "classB also loaded successfully!
"; 

  } 

 }

}

 代码如下 复制代码

 

function __autoload($class_name) {

require_once ("./".ucfirst($class_name).'.class.php');//ucfirst使类名首字母转换为大写

}

$obj = new ClassB();

$obj->funa(); 

$obj->funb(); 

?>

}

Then define another class ClassB in the same directory. The file name is ClassB.class.php. ClassB inherits ClassA

The code is as follows Copy code
class ClassB extends ClassA {


public function funb(){                                                            

echo "classB also loaded successfully!
";


}

}

Finally, define another autoload.php in the same directory as the above two files (you can choose this file name as you like)

The code is as follows Copy code
代码如下 复制代码

index.php

function __autoload($className){ //ucfirst() 将字符串首字母变为大写

include("ucfirst($className)".class.".php"); }

/**************

<🎜>function __autoload($class_name) { <🎜> <🎜> require_once ("./".ucfirst($class_name).'.class.php');//ucfirst converts the first letter of the class name to uppercase <🎜> <🎜>} <🎜> <🎜> $obj = new ClassB(); <🎜> <🎜>$obj->funa(); $obj->funb(); ?> Run the autoload.php program and see the results: classA loaded successfully! classB also loaded successfully! Example: There are several files in the folder: User.class.php; Person.class.php; Message.class.php; index.php; This is how we can do it
The code is as follows Copy code
index.php function __autoload($className){ //ucfirst() changes the first letter of the string to uppercase include("ucfirst($className)".class.".php"); } /******************

*For example: Instantiate the User class in User.class.php in the index.php file. If it does not exist, automatically call the __autoload() function

The code is as follows
 代码如下 复制代码

*,将类名User作为参数传入

*************/

$user=new User();

//通过自动加载类调用User.class.php文件

$person=new Person();

//通过自动加载类调用Person.class.php文件

$Message=new Message();

//通过自动加载类调用Message.classphp文件 function="" message="new" person="new" user="new">

Copy code
*, pass the class name User as a parameter

*************/

//Calling the User.class.php file through automatic loading of classes $person=new Person(); //Call the Person.class.php file through automatic loading class $Message=new Message(); //Calling the Message.classphp file through automatic loading of classes function="" message="new" person="new" user="new">
http://www.bkjia.com/PHPjc/632689.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632689.htmlTechArticleThis article will give you a brief introduction to the __autoload() method of automatically loading classes in PHP. I hope this method It will be helpful to all classmates. Function: When you need to instantiate a...
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