[英]Can PSR-0 class autoloader, load PEAR library?
So this is my question, I wrote my own class autoloader that fallows PSR-0 standard. 所以这是我的问题,我编写了自己的不符合PSR-0标准的类自动加载器。 That's easy.
这很容易。 Now I have my PHP MVC Frameowrk and I would like to use Twig , but Twig fallows PEAR naming standard that use
_
instead of \\
. 现在我有了PHP MVC Frameowrk,我想使用Twig ,但是Twig放弃了使用
_
代替\\
PEAR命名标准。
Now is it true that PSR-0 Autoloaders should also be able to load PEAR libs? 现在PSR-0自动加载器也应该能够加载PEAR库吗? Since inside those Autoloaders
_
is transformed into \\
? 由于在那些自动装带器中
_
转换为\\
? My loader can't load Twig if I register it, but it may be I made a mistake somewhere. 如果我注册了Twig,我的装载机就无法装载它,但可能是我在某个地方犯了一个错误。
Should PSR-0 compatible class loader be able to load PEAR libs too? 与PSR-0兼容的类加载器是否也应该能够加载PEAR库?
Should PSR-0 compatible class loader be able to load PEAR libs too?
与PSR-0兼容的类加载器是否也应该能够加载PEAR库?
Yes, PSR-0 is interoperable with the PEAR naming conventions of classes and files. 是的,PSR-0与类和文件的PEAR命名约定可以互操作。
This is not publicly documented any longer, but it is commonly known. 这不再是公开记录,但它是众所周知的。
If you can not load the classes, your autoloader is not PSR-0 compliant. 如果无法加载这些类,则表明您的自动加载器不符合PSR-0。
Double-Check with the specification . 仔细检查规格 。 You will also find a
SplClassLoader
class linked there that is PSR-0 compatible and you can use instead of your own. 您还会发现在
SplClassLoader
链接了一个与 PSR-0兼容的SplClassLoader
类 ,您可以使用它而不是自己的。
A probably better loader is The Symfony2 ClassLoader Component . 一个可能更好的加载器是Symfony2 ClassLoader Component 。 You can install it easily via Pear or Composer ( symfony/class-loader on Packagist ).
您可以通过Pear或Composer( Packagist上的symfony / class-loader )轻松安装它。
If you write your own classloader, take care you work with spl_autoload_register
, see 如果您编写自己的类加载器,请小心使用
spl_autoload_register
,请参见
To have spl_autoload_register
behaving just like in PHP but with PSR-0 resolution to include-path: 要使
spl_autoload_register
行为与PHP相似,但对包含路径的PSR-0分辨率为:
$spl_autoload_register_psr0 = function ($extensions = null) {
$callback = function ($className, $extensions = null) {
if (!preg_match('~^[a-z0-9\\_]{2,}$~i', $className)) {
return;
}
null !== $extensions || $extensions = spl_autoload_extensions();
$extensions = array_map('trim', explode(',', $extensions));
$dirs = array_map('realpath', explode(PATH_SEPARATOR, get_include_path()));
$classStub = strtr($className, array('_' => '/', '\\' => '/'));
foreach ($dirs as $dir) {
foreach ($extensions as $extension) {
$file = sprintf('%s/%s%s', $dir, $classStub, $extension);
if (!is_readable($file)) {
continue;
}
include $file;
return;
}
}
};
return spl_autoload_register($callback);
};
var_dump(get_include_path());
var_dump(spl_autoload_extensions());
var_dump($spl_autoload_register_psr0());
PSR-0 should be able to work with both full-qualified namespaces and underscored classes. PSR-0应该能够使用标准的名称空间和带下划线的类。 All you should need to do is replace the underscores with a directory seperator.
您需要做的就是用目录分隔符替换下划线。 A good example would be:
一个很好的例子是:
function autoload($className)
{
$className = ltrim($className, '\\');
$fileName = '';
$namespace = '';
if ($lastNsPos = strripos($className, '\\')) {
$namespace = substr($className, 0, $lastNsPos);
$className = substr($className, $lastNsPos + 1);
$fileName = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
}
$fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
require $fileName;
}
Hope this helps! 希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.