php获取包含文件的名称空间
问题描述:
//file foo.php
<?php
namespace foo;
class foo{
function __construct(){
echo "hello";
}
}
?>
//file index.php
<?php
require_once("foo.php");
echo __NAMESPACE__;
?>
我的问题是,从我的index.php文件中,是否可以在不读取文件内容并对其执行正则表达式的情况下知道foo.php的命名空间是什么?这似乎是很多开销.
My question is, from my index.php file, is it possible to know what the namespace of foo.php is without reading the file contents and doing a regular expression on it? That just seems like a lot of overhead.
///编辑
我真的很希望能够将命名空间动态添加到所包含的文件中.
I'd really like to be able to dynamically add the namespace to the included file.
<?php
namespace dynamic;
require_once("foo.php");
echo __NAMESPACE__;
?>
我想允许第三方插件,但是php名称空间似乎很糟糕.我不想让插件编辑器必须创建一个名称空间.
I want to allow for third-party plugins, but php namespaces seems terrible. I don't want to have the plugin editors have to create a namespace.
答
否.但是您可以这样欺骗它:
No. But you could trick it this way round:
//file foo.php
<?php
namespace foo;
//...
return __NAMESPACE__; // must be last line
?>
为了读出来:
//file index.php
<?php
$ns = require_once("foo.php");