在我在服务器端的include_path中,我在’/usr/share / pear /’中引用了一个pear目录.在我的应用程序中,我包含来自公共库的文件,位于’/usr/share / pear / library /’中,带有require_once’library / file.php’.
我最近开始使用spl自动加载器,我注意到在加载器函数中你必须确定包含文件的逻辑.我这样做的第一种方法是尝试包含一个文件并用@来抑制它以查看它是否会失败,例如@include’library / file.php’但是我认为主要是因为我读了很多关于@是一个不好的做法我决定通过PATH_SEPARATOR爆炸get_include_path并看看目录是否是我希望它来自己手动完成工作,然后做一个file_exists并包含它.
像这样:
function classLoader( $class ) {
$paths = explode( PATH_SEPARATOR, get_include_path() );
$file = SITE_PATH . 'classes' . DS . $class . '.Class.php';
if ( file_exists( $file) == false )
{
$exists = false;
foreach ( $paths as $path )
{
$tmp = $path . DS . 'library' . DS . 'classes' . DS . $class . '.Class.php';
if ( file_exists ( $tmp ) )
{
$exists = true;
$file = $tmp;
}
}
if ( !$exists ) { return false; }
}
include $file;
}
spl_autoload_register('classLoader');
我走错了路吗?我应该刚刚完成了@include业务,还是我在正确的方向上做了一些事情?