The following does not work:
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
But having all namespace works:
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);
}
Any idea how can I use the namespace declared above instead of in the function?
Thanks!
From the php.net comment section on constant()
:
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.
You have to use the full namespace path.