不允许将表达式作为字段默认值 [英] Expression is not allowed as field default value

查看:1882
本文介绍了不允许将表达式作为字段默认值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试让整个班级都可以使用$app.

I am trying to make $app available for the whole class.

首先,我得到:

不允许将表达式作为字段默认值"

"Expression is not allowed as field default value"

第二,在第5行,我得到:

Second, on line 5, I get:

未识别的变量$ app

Unidentified variable $app

我如何实现我的目标?

class UserController extends XController
{
    var $app = Yii::app();;
    public function init()
    {
        $test = $app;

推荐答案

您不能调用方法来为PHP中的变量设置默认值,即使它是静态方法也是如此.将其更改为在构造函数中设置:

You can not call a method to set a default value for a variable in PHP, not even if it is a static method. Change it to be set in the constructor:

use Yii;

class UserController extends XController    
{
    var $app;

    function __construct() {
        $this->app =  = Yii::app();
    }

    public function init()    
    {
        $test = $this->app;
    } 
}

作为旁注,您不应在PHP版本4以上使用var关键字,请参见

As a sidenote, you should not use the var keyword in PHP versions > 4, see this question for explanation.

这篇关于不允许将表达式作为字段默认值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆