Expression is not allowed as field default value

前端 未结 1 1163
攒了一身酷
攒了一身酷 2020-12-05 23:49

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

First, I get:

\"Expression is not allowed as field default value\"

1条回答
  •  粉色の甜心
    2020-12-06 00:36

    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;
        } 
    }
    

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

    0 讨论(0)
提交回复
热议问题