在服务器端的 include_path 中,我引用了 pear 目录,位于“/usr/share/pear/”中。在我的应用程序中,我包含来自公共库的文件,这些文件位于 '/usr/share/pear/library/' 中,带有require_once 'library/file.php'
.
我最近开始使用 spl 自动加载器,我注意到在加载器函数中你必须确定包含文件的逻辑。我这样做的第一种方法是尝试包含一个文件并抑制它@
以查看它是否会失败,例如,@include 'library/file.php'
但我认为主要是因为我阅读了很多关于@
这是一种不好的做法的信息,我决定自己手动完成这项工作get_include_path
。PATH_SEPARATOR
并查看目录是否是我想要的目录,然后执行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
业务,还是我在某种程度上朝着正确的方向做这件事?