PHP_autoload automatic loading class and mechanism analysis_PHP tutorial

WBOY
Release: 2016-07-21 14:58:27
Original
650 people have browsed it

When using PHP's OO mode to develop a system, it is usually customary to store the implementation of each class in a separate file. This will make it easy to reuse the class and maintain it in the future. Very convenient too. This is also one of the basic ideas of OO design. Before PHP5, if you need to use a class, you only need to include it directly using include/require

test.class.php

<?php <br />class abc{ <br />function __construct() <br />{ <br />echo 'www.hzhuti.com; <br />} <br />} <br />?>
Copy after login

load.php

The code is as follows Copy the code

<?php <br />class LOAD <br />{ <br />static function loadClass($class_name) <br />{ <br />$filename = $class_name.".class.php"; <br />if (is_file($filename)) return include_once $filename; <br />} <br />} <br />/** <br />* 设置对象的自动载入 <br />* spl_autoload_register &mdash; Register given function as __autoload() implementation <br />*/ <br />spl_autoload_register(array('LOAD', 'loadClass')); <br />$a = new Test();//实现自动加载,很多框架就用这种方法自动加载类 <br />?> 
Copy after login

__autoload()
In actual projects, it is impossible to write all classes in a PHP file. When you need to call a class declared in another file in a PHP file, you need to include it. This file is imported. 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.

See a complete example below

class ClassA{ <br>public function __construct(){ <br>echo “ClassA load success!”; <br>} <br>} <br>//定义一个类ClassA,文件名为ClassA.php <br>class ClassA{ <br>public function __construct(){ <br>echo “ClassA load success!”; <br>} <br>} <br>class ClassB extends ClassA { <br>public function __construct(){ <br>//parent::__construct(); <br>echo “ClassB load success!”; <br>} <br>} <br>//定义一个类ClassB,文件名为ClassB.php,ClassB继承ClassA <br>class ClassB extends ClassA { <br>public function __construct(){ <br>//parent::__construct(); <br>echo “ClassB load success!”; <br>} <br>} 
Copy after login

After defining two classes for testing, let’s write a PHP running program file containing the __autoload() method as follows:

function __autoload($classname){ <br>$classpath=”./”.$classname.'.php'; <br>if(file_exists($classpath)){ <br>require_once($classpath); <br>} <br>else{ <br>echo ‘class file'.$classpath.'not found!'; <br>} <br>}<p>$newobj = new ClassA(); <br>$newobj = new ClassB();</p>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/363838.htmlTechArticleWhen using PHP’s OO mode to develop systems, it is usually customary to store the implementation of each class in a In a separate file, this will make it easy to reuse the class and maintain it in the future...
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