I'm having trouble understanding why I can access a property from my parent class, but it's NULL, even though it has already been set by the parent (and has not been knowingly reset). I thought it might be because that property was set by a private method, but no difference when I changed to public. Here's a radically simplified example:
class TheParent
{
protected $_parent_property;
function __construct()
{}
private function parent_method($property);
{
$this->_parent_property = $property;
$call = new TheChild;
$call->child_method();
}
}
class TheChild extends TheParent
{
function __construct()
{
parent::construct();
}
public function child_method();
{
echo $this->_parent_property;
exit;
}
}
$test = new TheParent;
$test->parent_method('test');
I worked around this by passing the parent property to the child when the child is constructed by the parent ie new TheChild($this->_parent_property)
, but I still don't understand why $this->_parent_property is set to NULL when accessed from the child in my original example.
I do know that if I set this property from the parent constructor, I'd be able to access it just fine. I'm trying to understand why a property set by a parent method, and accessible by other parent methods, is not accessible from the child class which extends the parent.
Can anyone explain? Thanks!