Php constant() 不适用于命名空间?

标签 php namespaces class-constants

以下不起作用:

use application\components\auditor\AuditLevel;


public function actionAudit()
{
    $data=unserialize($_POST['data']);
    $message=$data['message'];
    $context=$data['context'];
    $level=$context['level'];
    Yii::app()->auditor->log(constant('AuditLevel::'.$level), $message, $context);
}

// constant(): Couldn't find constant AuditLevel::INFO

但是让所有命名空间都有效:
use application\components\auditor\AuditLevel;


public function actionAudit()
{
    $data=unserialize($_POST['data']);
    $message=$data['message'];
    $context=$data['context'];
    $level=$context['level'];
    Yii::app()->auditor->log(constant('application\components\auditor\AuditLevel::'.$level), $message, $context);
}

知道如何使用上面声明的命名空间而不是在函数中吗?

谢谢!

最佳答案

来自 constant() 上的 php.net 评论部分:

As of PHP 5.4.6 constant() pays no attention to any namespace aliases that might be defined in the file in which it's used. I.e. constant() always behaves as if it is called from the global namespace.



您必须使用完整的命名空间路径。

关于Php constant() 不适用于命名空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20952904/

相关文章:

C++ 如何在静态库的命名空间中设置私有(private)类成员?

php - 在 PHP 中动态访问类常量

c++ - 这个静态类字段在创建之前就被使用了吗?

C# 构造函数和常量之谜

php - 用php动态生成mysql查询

php - PHP和C++之间的套接字连接

C++ 名称解析

php - 所有可能的 PHP 错误列表

php - mysqli 根据行ID值显示数据

typescript 多文件命名空间导出错误