我在spl_autoload_register()
注册函数中。在这里,我可以获取类名,但是如何确定它是哪个命名空间?
答案 0 :(得分:2)
试试这个例子。您可以定义一个处理呼叫的类
<?php
namespace Example;
#Define a classLoader object
class Loader{
/**
* Construct and set the autoloader class handler
*/
public function __construct() {
spl_autoload_register(array($this, 'loadClass'));
}
/**
* This method is called when an object is loaded
*
* @param string $args
*/
public function loadClass($args){
var_dump($args);
//REMOVE: this is only for killing the process
die;
}
}
//Create your autoloader object
new Loader();
//Calling some class
new \Some\Name\Space\Example();
你应该得到这样的回应。
string(23) "Some\Name\Space\Example"
答案 1 :(得分:1)
如果您想要当前的命名空间:
namespace MyProject;
echo '"', __NAMESPACE__, '"'; // outputs "MyProject"
否则你存储在配置文件中或(通过应用程序结构的注释中说明)我想...