
Originally Posted by
33degrees
Autoload isn't designed to do what you're trying to, it's for classes only.
Thank you.
I need a solutions for autoload namespace.
PHP Code:
use Namespace1; // => auto include Namespace1.php
// good
Namespace1::test(); // => auto include Namespace1/test.php
http://www.onphp5.com/article/61
PHP Code:
<?php
function __autoload($className) {
// Assume that all class files are located in the same dir and subdirs
$fname = str_replace('::', DIRECTORY_SEPARATOR, $className) . '.php';
if(is_file($fname)) {
include_once($fname);
return;
}
$namespace = substr($className, 0, strrpos($className, '::'));
$localClassName = substr($className, strrpos($className, '::') + 2);
if($namespace) {
eval("namespace $namespace;
class $localClassName {
function __construct() {
throw new Exception('Class $namespace::$localClassName not found');
}
static function __callstatic(\$m, \$args) {
throw new Exception('Class $className not found');
}
}");
} else {
eval("class $className {
function __construct() {
throw new Exception('Class $className not found');
}
static function __callstatic(\$m, \$args) {
throw new Exception('Class $className not found');
}
}");
}
}
Bookmarks