サーバー側のinclude_pathには、「/ usr / share /pear/」にある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
ビジネスをするべきでしたか、それとも私はそれをいくらか正しい方向にやっていますか?