In the following code, I want to assign an $settings
's key, help
to a constant class variable value, $To_default
. However, I am told Constant expression contains invalid operations
. Is there any way I can get around this?
class EmailClass extends PluginBase {
private $To_default = 'scrollout@stackoverflow.com';
protected $settings = array(
'To'=>array(
'type'=>'text',
'label'=>'To',
'help'=>$this->To_default, // Constant expression contains invalid operations
),
);
I've tried declaring $To_default
in various ways including private const $To_default
, static private $To_default
, etc. but none worked. $settings
is not static, as you can see, so I don't understand why this is a problem.
Don't know deep technical explanation for this but I think this is because normally you initialize properties in the constructor, like so:
class EmailClass
{
private $To_default = 'scrollout@stackoverflow.com'; // not a constant, so better throw it into constructor too
protected $settings;
public function __construct() {
$this->settings = array(
'To'=>array(
'type'=>'text',
'label'=>'To',
'help'=>$this->To_default
)
);
}
}
Analogical I don't know why: 'if isset(expression)' doesn't work but it doesn't have to. There's a better solution: 'if(expression)'. This is just how we do it.