Inheritance of properties is possible when a property is hardcoded. See below:
class ParentObj {
protected $familyName = 'Lincoln';
}
class ChildObj extends ParentObj {
public function __construct() {
var_dump($this->familyName);
}
}
$childObj = new ChildObj();
// OUTPUT
string 'Lincoln'
Inheritance of properties is not possible when a property is dynamic. See below:
class ParentObj {
protected $familyName;
public function setFamilyName($familyName){
$this->familyName = $familyName;
}
}
class ChildObj extends ParentObj {
public function __construct() {
var_dump($this->familyName);
}
}
$familyName = 'Lincoln';
$parentObj = new ParentObj();
$parentObj->setFamilyName($familyName);
$childObj = new ChildObj();
// OUTPUT
null
So the question is: Why is not possible for a child class to inherit properties class that are set dynamically?