Mar*_*ace 19 php autoload psr-0
我有一个小应用程序,我需要一个自动加载器.我可以很容易地使用symfony2类加载器,但它似乎有点矫枉过正.
那里有一个稳定的极轻量级psr-0自动加载器吗?
Adr*_*rat 28
你要求非常轻量级,让我们这样做;)
蒂莫西Boronczyk写了一个很好的最小SPL自动加载磁带机:http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html
我压缩了这样的代码:
function autoload1( $class ) {
preg_match('/^(.+)?([^\\\\]+)$/U', ltrim( $class, '\\' ), $match ) );
require str_replace( '\\', '/', $match[ 1 ] )
. str_replace( [ '\\', '_' ], '/', $match[ 2 ] )
. '.php';
}
Run Code Online (Sandbox Code Playgroud)
然后将此[autoload3]与短的@Alix Axel代码 [autoload4] 进行比较(缩小版本):
function autoload3($c){preg_match('/^(.+)?([^\\\\]+)$/U',ltrim($c,'\\'),$m);require str_replace('\\','/',$m[1]).str_replace(['\\','_'],'/',$m[2]).'.php';}
function autoload4($c){require (($n=strrpos($c=ltrim($c,'\\'),'\\'))!==false?str_replace('\\','/',substr($c,0,++$n)):null).str_replace('_','/',substr($c,$n)).'.php';}
Run Code Online (Sandbox Code Playgroud)
autoload3是最短的!
让我们使用稳定且极其轻量级(175b!)的自动加载器文件:
<?php spl_autoload_register(function ($c){preg_match('/^(.+)?([^\\\\]+)$/U',ltrim($c,'\\'),$m);require str_replace('\\','/',$m[1]).str_replace(['\\','_'],'/',$m[2]).'.php';});
Run Code Online (Sandbox Code Playgroud)
也许我疯了,但你问极端,不是吗?
编辑:感谢Alix Axel,我缩短了代码(只有100b!)并且使用了include而不是require,以防你有各种自动加载策略用于旧库(然后在spl自动加载堆栈中使用各种自动加载器......).
<?php spl_autoload_register(function($c){@include preg_replace('#\\\|_(?!.+\\\)#','/',$c).'.php';});
Run Code Online (Sandbox Code Playgroud)
如果你想让它更短/更好,请使用这个要点.
hak*_*kre 15
在PSR-0规范文档具有发明实施例一兼容自动加载功能已经很短:
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;
}
Run Code Online (Sandbox Code Playgroud)
它的使用非常简单:
spl_autoload_register('autoload');
Run Code Online (Sandbox Code Playgroud)
它的缺点是,您需要使用该include_path
指令配置它所使用的基目录.为了支持混合PSR-0自动加载器倾向于SPL语义,以下一个支持包括路径和spl自动加载扩展:
$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);
};
Run Code Online (Sandbox Code Playgroud)
在该Symfony2的类加载器组件必须允许更多的在这里配置的好处.您可以通过Pear或Composer(Packagist上的symfony/class-loader)轻松安装它.它是一个独立的组件,被许多人使用,并且经过了相当好的测试和支持.