Home  >  Article  >  Backend Development  >  php_autoload automatic loading class and mechanism analysis_PHP tutorial

php_autoload automatic loading class and mechanism analysis_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:21:13893browse

Before PHP5, if you need to use a class, you only need to include it directly using include/require
test.class.php

Copy code The code is as follows:

class abc{
function __construct()
{
echo 'www.hzhuti.com;
}
}
?>

load.php
The code is as follows
Copy codeThe code is as follows:

class LOAD
{
static function loadClass($class_name)
{
$filename = $class_name.".class.php" ;
if (is_file($filename)) return include_once $filename;
}
}
/**
* Set the automatic loading of objects
* spl_autoload_register — Register given function as __autoload() implementation
*/
spl_autoload_register(array('LOAD', 'loadClass' ));
$a = new Test(); // Implement automatic loading. Many frameworks use this method to automatically load classes
?>

__autoload()
In actual projects, it is impossible to write all classes in one PHP file. When a class declared in another file needs to be called in a PHP file, the file needs to be introduced through include. However, sometimes, in projects with many files, it is necessary to include all required class files one by one. A big trouble is having to write a long list of included files at the beginning of each class file. Can we import the php file where this class is located when we use it?
For this purpose, PHP provides the __autoload() method, which 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.
One parameter received by the __autoload() method is the class name of the class to be loaded, so at this time the class name needs to correspond to the file name, such as Person.php, the corresponding class name is Pserson.
Look at a complete example below
Copy the code The code is as follows:

class ClassA{
public function __construct (){
echo “ClassA load success!”;
}
}
//Define a class ClassA, the file name is ClassA.php
class ClassA{
public function __construct (){
echo “ClassA load success!”;
}
}
class ClassB extends ClassA {
public function __construct(){
//parent::__construct() ;
echo “ClassB 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!”;
}
}

Define two After testing the class, we write a PHP running program file containing the __autoload() method as follows:
Copy the code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/324985.htmlTechArticleBefore PHP5, if you need to use a class, you only need to directly use include/require to include it for testing .class.php Copy the code as follows: ?php class abc{ function __const...
Statement:
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