Who can explain why this returns an error:
$test = new myclass();
class myclass {
private $object = (object) NULL;
public function addmember() {
$this->object->member1 = 'member 1';
}
}
$test -> addmember();
... and this is OK:
$test = new myclass();
class myclass {
private $object = '';
public function addmember() {
$this->object = (object) NULL;// new stdClass();
$this->object->member1 = 'member 1';
}
}
$test -> addmember();
But why? Who can explain why the first example is wroing? Why I have to put the line with "(object)NULL" IN the function?
Expressions are not allowed in a class body definition.
From php.net:
For example, you can not do this:
But can do this:
Also, you can initialize property within a class body by constant value, that does not need to be evaluated on parse process: