Blogger Information
Blog 55
fans 0
comment 0
visits 50473
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
PHP-类的自动加载-0905
Bean_sproul
Original
624 people have browsed it

spl_autoload_register函数是实现自动加载未定义类功能的的重要方法,所谓的自动加载意思就是 我们的new 一个类的时候必须先include或者require的类文件,如果没有include或者require,则会报错。

类的自动加载

方法1

include    ‘路径 ’;  即使引入失败,继续执行后面的代码

require    ‘路径 ’; 引入失败,直接结束,一般用来引用数据库config.php等


方法2

spl_autoload_register


实例

<php
//常规加载
spl_autoload_register(function ($className){
    require './class/'.$className.'.php';
    
//存在命名空间的情况下
//$className = str_replace("\\","/", $className);
//require './class/'.$className.'.php';

});

echo Demo1::CLASS_NAME, '<hr>';
echo Demo2::CLASS_NAME, '<hr>';

运行实例 »

点击 "运行实例" 按钮查看在线实例





Correction status:Uncorrected

Teacher's comments:
Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post