以下不起作用:
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/