I have a parent class I'll be calling 'ParentClass' and a child class (that extends from it) which I'll be calling 'ChildClass'.
ParentClass has protected properties $prop1 and $prop2 which I want ChildClass to access. But I'm getting NULL from them.
ParentClass has a __construct() method which sets the properties received through dependency injection.
ParentClass instantiates ChildClass from one of its methods.
ChildClass overwrites the parent constructor, but doesn't contain any code inside of its own __construct() method.
I have already tested the properties at the parent class with var_dump($this->prop1). It returns the value I'm expecting.
However, if I var_dump($this->prop1) from the child class, I get NULL.
class ParentClass {
protected $prop1;
protected $prop2;
public function __construct($prop1, $prop2) {
$this->prop1 = $prop1;
$this->prop2 = $prop2;
}
public function fakeMethod() {
$child = new ChildClass;
$child->anotherFakeMethod();
// logic
}
}
class ChildClass extends ParentClass {
public function __construct() {
// this overrides the parent constructor
}
public function anotherFakeMethod() {
$prop1 = $this->prop1;
$prop2 = $this->prop2;
var_dump($this->prop1);
// returns NULL
}
}
Why can't the child class access the parent class' properties if it extends from it?