[英]CakePHP 2 access to model constant from controller method
I have a Cakephp 2 model with a class constant:我有一个带有类常量的 Cakephp 2 模型:
class Person extends AppModel
{
const NAME = 'MyName';
}
How can i access this class constant into the controller method ?如何将此类常量访问到控制器方法中?
I try :我尝试:
public function SayName( $id )
{
var_dump($this->Person->NAME);
die;
}
But the result was : NULL但结果是:NULL
It's a bit strange, but you'll want to do this:这有点奇怪,但你会想要这样做:
$person = $this->Person;
var_dump($person::NAME);
PHP doesn't like the format $this->inst::CONSTANT
, so simply setting it as a variable (like above) will do the trick. PHP 不喜欢$this->inst::CONSTANT
格式,因此只需将其设置为变量(如上)即可。
You can also declare your model usage at the top of the controller with the command您还可以使用以下命令在控制器顶部声明您的模型用法
App::uses('Person', 'Model');
Then you can access the model constant in the controller with然后你可以访问控制器中的模型常量
Person::NAME
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.