PHP namespace and autoload doesn't work -
i have file called query.php.
namespace test { class query { public function __construct() { printf("hello, world"); } } }
in bootstrap.php seek phone call it:
spl_autoload_register(function($classname) { if(file_exists('../folder/'.$classname.'.php')) { require_once '../folder/'.$classname.'.php'; } }); new \test\query();
result: fatal error: class test\query not found.
without namespace works fine. how prepare it?
thanks in advance.
you have replace \
directory_separator
.
define('base_path', realpath(dirname(__file__))); spl_autoload_register(function($classname) { if(file_exists(base_path . '../folder/'. str_replace('\\', directory_separator , $class). '.php')) { require_once '../folder/'.$classname.'.php'; } });
this assuming directory construction following
/folder/boostrap.php
/folder/test/query.php
see how utilize php namespaces autoload?
php namespaces autoload
No comments:
Post a Comment