Minimal example:
class Equipment
{
public $Temp;
function __construct($filter) {
$this->Temp = 100;
if(ContainsStringHelper($filter, "car")) {
$this->Running = true;
}
}
}
You can see that I want to define $this->Running
if the $filter
variable contains the word car
. If it does not, I don't want the property on the object.
This works, however PHPStorm throws a problem saying "Property declared dynamically":
I can get rid of that by declaring the property public $Running
, but I don't want it to be a property on the object as null
if the logic doesn't assign it a value.
Is there a proper way to do this?
CodePudding user response:
A solution can be to declare the property and unset it during construction in your condition :
class Equipment
{
public $Temp;
public $Running = NULL;
function __construct($filter) {
$this->Temp = 100;
if(ContainsStringHelper($filter, "car")) {
$this->Running = true;
} else {
unset($this->Running) ;
}
}
}