有没有办法获得 PHP 命名空间,以允许在命名空间内调用函数,就好像它们是全局函数一样?
例子:
handle()
而不是:
prggmr\handle()
这是我的示例代码:
<?php
require_once 'prggmr/src/prggmr.php';
prggmr\handle(function() {
echo 'This is a test scenario';
}, 'test');
prggmr\signal('test');
别名不适用于函数:
<?php
require 'prggmr/src/prggmr.php';
use prggmr\handle;
handle(function(){
echo "Test";
}, "test");
结果:
Fatal error: Call to undefined function handle()
最佳答案
根据我的理解,文档摘录表明这是不可能的......
Neither functions nor constants can be imported via the use statement.
来源:
http://www.php.net/manual/en/language.namespaces.faq.php#language.namespaces.faq.nofuncconstantuse
关于PHP 命名空间全局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10231809/