这取决于你想要完成什么。
如果你想在文件和它们所在的目录之间有一个可配置的映射,你需要制定一个路径抽象并实现一些加载函数来处理它。我会做一个例子。
假设我们将使用诸如Core.Controls.Control
这样的符号来指代将在(逻辑)目录Core.Controls
中找到的(物理)文件Control.php
。我们将需要做两部分实现:
- 指导我们的装载机
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');
当然,这个例子是最起码的,做一些有用的东西,但你可以看到它允许你做什么。
道歉,如果我犯了一些小错误,我正在打字,因为我走了。 :)
来源
2011-03-23 02:11:36
Jon
我也有这个问题,我已经到了PHP的结果,真的没有一个很好的包系统。 Netbeans虽然有帮助。 – 2011-03-23 02:10:03