Expression Is Not Allowed as Field Default Value

Expression is not allowed as field default value

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.

Expression is not allowed as field default value (on oop)

public $bdd = parent::conn();

You can't set property value from a function in the property declaration

You must initialize property value in methods, for example in constructor

class query extends connection
{
public $bdd;

public function __construct()
{
parent::__construct();
$this->bdd = parent::conn();
}
}

Expression is not allowed in a variable outside of a function

Move it into to the constructor (or any other function) - you seem not to have any constructor for the class, but __construct will be called anyway :

protected $screenshotPath = '';

public function __construct() {
$this->$screenshotPath = __DIR__ . "/FailedTestsScreenshots";
}

or into SetUp() :

protected function SetUp() {
$this->$screenshotPath = __DIR__ . "/FailedTestsScreenshots";
$this->setBrowser( "*firefox" );
$this->SetBrowserUrl( $this->path );
}

How can I use an expression as parameter default value?

It's not possible, but if you're looking for more condensed solution in PHP 7 you can use new ?? operator:

echo $m ?? config('my.status.mode');

As alternative, you can create your own global Laravel helper, wrapper for config() helper which will check variable for null and will return result from config:

$m = default('status.mode', $m);

Helper can look like this:

if (! function_exists('default')) {
function default($config, $value)
{
return is_null($value) ? config('my.'.$config) : $value;
}
}

An advantage of this method is you can add some functionality to this helper at any time. For example you could get config value translated to the current language without doing this in a controller or a model.

Can I use a function for a default value in MySql?

No, you can't.

However, you could easily create a trigger to do this, such as:


CREATE TRIGGER before_insert_app_users
BEFORE INSERT ON app_users
FOR EACH ROW
SET new.api_key = uuid();


Related Topics



Leave a reply



Submit