【发布时间】:2014-01-17 04:30:31
【问题描述】:
在 python 中,您可以执行“import star”将其他命名空间中的所有内容嵌入到本地命名空间中:
# file "spam":
def ham():
some code
# file "main":
from spam import *
xy = ham() <-- uses 'ham' from 'spam'
这通常被认为是“不好的做法”,但我仍然想知道如何对 php 命名空间做同样的事情。以下不起作用:
# file "spam":
namespace spam;
function ham() {
some code
}
# file "main":
include "spam.php";
use spam;
xy = ham() <-- error
【问题讨论】:
-
这很奇怪......我每天都这样做,而且每次都有效。用函数(或公共函数)a制作页面a.php,将其包含在页面b.php中并调用a函数。
-
@Goikiu:好点子,但我想保留“垃圾邮件”的命名空间,以便使用它的人有选择。
-
“函数和常量都不能通过use语句导入。” - php.net/…
标签: php namespaces