Home  >  Article  >  php教程  >  PHP 自动引入一个目录的所有 PHP 文件

PHP 自动引入一个目录的所有 PHP 文件

WBOY
WBOYOriginal
2016-05-25 16:45:571009browse

我爱水煮鱼有个微信插件,会时不时的退出一些插件的 "插件",直接下载一个提供的 PHP 文件,上传到插件一个指定的目录即可,插件会自动检测、引入并执行这个文件,研究了下,也想弄一个类似的功能,代码如下:

/* 
 *引入一个目录的所有文件 
 *http://www.phprm.com 
*/ 
function Bing_include_all_php( $folder ){ 
 foreach( glob( "{$folder}/*.php" ) as $filename ) require_once $filename; 
}

使用方法就是直接调用这个函数,第一个参数放上要引入的相对目录即可,还有一种办法可以使用魔术方法__autoload来加载,代码如下:

set_include_path('aa' . PATH_SEPARATOR . get_include_path()); 
function __autoload($className) 
{ 
    //如果加这个检测, 因为此文件不在当前目录下,它就会检测不到文件存在,  
    //但include是能成功的 
    if (file_exists($className . '.php')) { 
        include_once($className . '.php'); 
    } else { 
        exit('no file'); 
    } 
} 
$a = new Acls();

 

我们一般使用_autoload自动加载类如下:

function __autoload($class_name) {    
    require_once ($class_name . "class.php");    
}    
$memo= new Demo();


永久链接:

转载随意!带上文章地址吧。

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