PHP SPL autoload
<?php
/* Directory:
/bootstrap.php
/index.php
/src/HTTP/Client.php
/src/Twitter/Client.php
*/
// bootstrap.php
// autoload with anonymous function
spl_autoload_register( function ($class) {
$classPath = str_replace('\\', '/' , $class ) ;
include _DIR_ . '/src/' . $classPath . '.php' ;
});
// class Client
namespace HTTP;
class Client {
// code ...
}
// class Twitter
namespace Twitter
class Twitter {
// code ...
}
ini_set('display_errors', 1) ;
//include './src/HTTP/Client.php';
//include './src/Twitter/Client.php';
// instead of including the 2 includes above:
include './bootstrap.php';
// specify their namespaces
var_dump(new HTTP\Client) ;
var_dump(new Twitter\Client) ;