Sunday 24 December 2017

php - Syntax error when trying to create array of anonymous functions

You must define the methods in the constructor, or some
other method, not directly in the class member
declaration.



class User extends
Model {

public $hasOne =
array('UserSetting');


public $validate =
array();

public $virtualFields = array();


public function __construct() {
$this->virtualFields = array(

'fullname' => function () {
return $this->fname . ($this->mname ? '
' . $this->mname : '') . ' ' . $this->lname;
},

'official_fullname' => function () {


}

);

}
}


While
that works, PHP's magic method __get() is better suited to this
task:



class User extends Model
{


public $hasOne =
array('UserSetting');

public $validate =
array();

public function __get($key) {
switch ($key)
{
case 'fullname':
return $this->fname . ($this->mname ? ' '
. $this->mname : '') . ' ' . $this->lname;

break;


case 'official_fullname':
return
'';
break;
};

}
}

No comments:

Post a Comment

php - file_get_contents shows unexpected output while reading a file

I want to output an inline jpg image as a base64 encoded string, however when I do this : $contents = file_get_contents($filename); print &q...