我想使用一个使用名称空间的css解析器。我把文件放在供应商和应用程序导入它。但脚本本身似乎找不到它的类
在我的类的顶部,我导入文件:
App::import('Vendor', 'Sabberworm', array('file' => 'Sabberworm/CSS/Parser.php'));
它在/root/vvendors/Sabberworm/CSS/中(所有文件都在这个命名空间中)
在我的类方法中,我创建了一个新实例:
public function parse($content) {
$oParser = new Sabberworm'CSS'Parser($content);
...
}
到目前为止还不错。但如果我现在想调用$oCss = $oParser->parse();
,它会出现致命错误:
"Fatal error: Class 'Sabberworm'CSS'CSSList'Document'"
它会失败,因为它需要其他文件(应该使用名称空间加载)。根供应商文件夹位于include路径中,外部脚本似乎将命名空间设置为"namespace Sabberworm''CSS;"。我错过了什么?我对名称空间有点陌生。
将其添加到引导
spl_autoload_register(function($class) {
foreach(App::path('Vendor') as $base) {
$path = $base . str_replace('''', DS, $class) . '.php';
if (file_exists($path)) {
return include $path;
}
}
});
或者只是这个功能:
$path = APP . 'Vendor' . DS . str_replace('''', DS, $class) . '.php';
if (file_exists($path)) {
include $path;
}