PHP - most lightweight psr-0 compliant autoloader
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.
Can PSR-0 class autoloader, load PEAR library?
PSR-0 should be able to work with both full-qualified namespaces and underscored classes. 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!
php psr-0 restful webservice library
Silex is very similar to Slim but with much better architecture IMO and it is psr-0 compliant.
Php syntax error syntax error, unexpected '[', expecting ')' str_replace, PSR-0 Autoloader
You are using php 5.4 array notation, it's not supported in lower PHP versions.
Change it to
$classname = str_replace("\\", "/", $match[1])
. str_replace(array("\\", "_"), "/", $match[2])
. ".php";
Related Topics
Arabic Characters in JSON Decoding
Check If Laravel Model Got Saved or Query Got Executed
Required_If Laravel 5 Validation
Disable Csrf in Laravel for Specific Route
Require_Once :Failed to Open Stream: No Such File or Directory
Setting Post Variable Without Using Form
Using Laravel Socialite to Login to Facebook
Connection Reset by Peer: Mod_Fcgid: Error Reading Data from Fastcgi Server
PHP MySQL - Insert New Record into Table with Auto-Increment on Primary Key
Wordpress Plugin -> Call to Undefined Function Wp_Get_Current_User()
Rationale Behind Simplexmlelement's Handling of Text Values in Addchild and Addattribute