Expression not allowed by default field value

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

First, I get:

"Expression not allowed as default field

Secondly, on line 5, I get:

Unrecognized $ app variable

How can I achieve my goal?

 class UserController extends XController { var $app = Yii::app();; public function init() { $test = $app; 
+16
source share
1 answer

You cannot call a method to set the default value for a variable in PHP, even if it is a static method. Change it to 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 note, you should not use the var keyword in PHP versions> 4; see this question for an explanation.

+25
source

All Articles