それはあなたが正確に何を達成しようとしているのかによります。
ファイルとファイルが存在するディレクトリの間に構成可能なマッピングが必要な場合は、パスの抽象化を実行し、それを処理するためにいくつかのローダー関数を実装する必要があります。例を示します。
(論理)ディレクトリにあるCore.Controls.Control
(物理)ファイルを参照するような表記法を使用するとします。2つの部分からなる実装を行う必要があります。Control.php
Core.Controls
Core.Controls
物理ディレクトリにマップされているローダーに指示します/controls
。
Control.php
そのディレクトリで検索します。
だからここにスタートがあります:
class Loader {
private static $dirMap = array();
public static function Register($virtual, $physical) {
self::$dirMap[$virtual] = $physical;
}
public static function Include($file) {
$pos = strrpos($file, '.');
if ($pos === false) {
die('Error: expected at least one dot.');
}
$path = substr($file, 0, $pos);
$file = substr($file, $pos + 1);
if (!isset(self::$dirMap[$path])) {
die('Unknown virtual directory: '.$path);
}
include (self::$dirMap[$path].'/'.$file.'.php');
}
}
次のようにローダーを使用します。
// This will probably be done on application startup.
// We need to use an absolute path here, but this is not hard to get with
// e.g. dirname(_FILE_) from your setup script or some such.
// Hardcoded for the example.
Loader::Register('Core.Controls', '/controls');
// And then at some other point:
Loader::Include('Core.Controls.Control');
もちろん、この例は何か便利なことをするための最低限のものですが、それが何をすることができるかを見ることができます。
小さな間違いをした場合はお詫びします。これを入力します。:)