Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Expression is not allowed as field default value

Tags:

oop

php

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

First, I get:

"Expression is not allowed as field default value"

Second, on line 5, I get:

Unidentified variable $app

How can I achieve my goal?

class UserController extends XController {     var $app = Yii::app();;     public function init()     {         $test = $app; 
like image 211
Vasil Gerginski Avatar asked Jan 27 '16 12:01

Vasil Gerginski


People also ask

What is the default value of a field?

Default field values automatically insert the value of a custom field when a new record is created. You can use a default value on a formula for some types of fields or exact values, such as Checked or Unchecked for checkbox fields. After you have defined default values: The user chooses to create a new record.


1 Answers

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.

like image 83
bpoiss Avatar answered Sep 19 '22 11:09

bpoiss