PHP - 最轻量级的 psr-0 兼容自动加载器 [英] PHP - most lightweight psr-0 compliant autoloader

查看:26
本文介绍了PHP - 最轻量级的 psr-0 兼容自动加载器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要自动加载器的小应用程序.我可以轻松地使用 symfony2 类加载器,但它似乎有点矫枉过正.

是否有稳定的极轻的 psr-0 自动加载器?

解决方案

你问的极其轻量级,让我们这样做吧;)

Timothy Boronczyk 写了一个不错的最小 SPL 自动加载器:http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html

我是这样压缩代码的:

function autoload1( $class ) {preg_match('/^(.+)?([^\\]+)$/U', ltrim( $class, '\' ), $match ) );要求 str_replace( '\', '/', $match[ 1 ] ).str_replace( [ '\', '_' ], '/', $match[ 2 ] ).'.php';}

然后将此 [autoload3] 的(缩小版本)与 简短的 @Alix Axel 代码 [autoload4] 进行比较:

function autoload3($c){preg_match('/^(.+)?([^\\]+)$/U',ltrim($c,'\'),$m);需要 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';}

autoload3 是最短的!

让我们使用稳定的 &非常轻量级(175b!)自动加载文件:

<?php spl_autoload_register(function ($c){preg_match('/^(.+)?([^\\]+)$/U',ltrim($c,'\'),$m);需要 str_replace('\','/',$m[1]).str_replace(['\','_'],'/',$m[2]).'.php';});

也许我疯了,但你要求极端,不是吗?

感谢 Alix Axel,我缩短了代码(只有 100b !)并使用 include 而不是 require,以防您对旧库有各种自动加载策略(然后是各种自动加载器)spl 自动加载堆栈...).

<?php spl_autoload_register(function($c){@include preg_replace('#\|_(?!.+\)#','/',$c).'.php';});

如果你想让它更短/更好,请使用这个gist.>

I have a tiny application that i need an autoloader for. I could easily use the symfony2 class loader but it seems like overkill.

Is there a stable extremely lightweight psr-0 autloader out there?

解决方案

You ask extremely lightweight, let's do so ;)

Timothy Boronczyk wrote a nice minimal SPL autoloader : http://zaemis.blogspot.fr/2012/05/writing-minimal-psr-0-autoloader.html

I condensed the code like this:

function autoload1( $class ) {
    preg_match('/^(.+)?([^\\]+)$/U', ltrim( $class, '\' ), $match ) );
    require str_replace( '\', '/', $match[ 1 ] )
        . str_replace( [ '\', '_' ], '/', $match[ 2 ] )
        . '.php';
}

Then compare (minified versions of) this [autoload3] with short @Alix Axel code [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';}

autoload3 is the shortest !

Let's use stable & extremely lightweight (175b !) autoloader file :

<?php spl_autoload_register(function ($c){preg_match('/^(.+)?([^\\]+)$/U',ltrim($c,'\'),$m);require str_replace('\','/',$m[1]).str_replace(['\','_'],'/',$m[2]).'.php';});

Maybe i'm crazy but you Asked for extreme, no?

EDIT: Thanks to Alix Axel, i've shorten the code (only 100b !) and used include instead of require in case you have various autoloading strategy for old libs (and then various autoloader in spl autoload stack...).

<?php spl_autoload_register(function($c){@include preg_replace('#\|_(?!.+\)#','/',$c).'.php';});

If you want to make it shorter / better, please use this gist.

这篇关于PHP - 最轻量级的 psr-0 兼容自动加载器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆