PHP当使用类名不存在时自动加载该类

PHP之自动加载

_autoload("fun_name");  ---->php7以下

spl_autoload_registor();  --->php7以上

 

 1 利用_autoload()实现自动加载
 2 //  头部使用函数:
 3         _autoload("find_target");
7 function find_target($className) 8 { 9 //构建文件类的路径 10 $ruls_array = array( 11 "./Lib/$className.clss.php", 12 "./thirdParty/$className.modle.php" 13 ..... 14 ); 15 foreach ($ruls_array as $key=>$values) 16 { 17 if(file_exists($file_path)) { 18 require_once($filename); 19 return; 20 } 21 } 22 23 } 24 25 $obj1 = new DBClickhouse(); --->当前文件不存在该类时,调用_autoload()函数,我们注册了 26 了 find_target 函数来处理、加载文件

 

posted @ 2020-05-05 23:12  G1733  阅读(505)  评论(0编辑  收藏  举报