PHP 命名空间全局

标签 php namespaces

有没有办法获得 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/

相关文章:

javascript - 如何避免 Javascript 中的长命名空间

c# - 如何在 WCF 服务中将方法组织/构建到命名空间中?

php - 用于清除 session 的 CodeIgniter 函数

php - 遍历特定行sql php

javascript - 使用 AJAX 简单调用 php 脚本不起作用

c++ - Visual Studio 和 Clang 不同的行为模板元编程

python - 作用域静态确定动态使用是什么意思?

php - 如何在 PHP 中获取有用的错误消息?

php - 引用 - 这个错误在 PHP 中是什么意思?

xml - 使用 LibXML-Ruby 清除不需要的命名空间