php获取包含文件的名称空间 [英] php get namespace of included file
本文介绍了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");
这篇关于php获取包含文件的名称空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文