这取决于您要准确完成的任务。
如果你想在文件和它们所在的目录之间有一个可配置的映射,你需要制定一个路径抽象并实现一些加载器函数来处理它。我会做一个例子。
假设我们将使用一个符号Core.Controls.Control
来引用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');
当然,这个例子是做一些有用的事情的最低限度,但你可以看到它允许你做什么。
抱歉,如果我犯了任何小错误,我正在打字。:)