Home > Backend Development > PHP Tutorial > PHP tutorial: autoload automatic loading class_PHP tutorial

PHP tutorial: autoload automatic loading class_PHP tutorial

WBOY
Release: 2016-07-13 10:27:43
Original
934 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. It will also be convenient for future maintenance. 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 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.

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 style="border: 0px; padding: 0px; margin: 1.3em 0px; ">$newobj = new ClassA(); <br>$newobj = new ClassB();</p>
Copy after login

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/815786.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