在服务器端的include_path中,我有一个对pear目录的引用,即'/usr/share/pear/'。在我的应用程序中,我从一个公共库中包含文件,位于'/usr/share/pear/library/'中,使用
我最近开始使用spl自动加载器,我注意到在加载器函数中,您必须确定包含文件的逻辑。我第一次尝试的方法是尝试包含一个文件,并使用
就像这样:
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
相关的业务,还是我正在朝着正确的方向努力?