I have been tinkering around with OOP and seeing how to go about doing this. I have parent class that does stuff and sets values to its own properties. Then I want to create a child class that extends the parent while “somehow” accessing the dynamically modified properties. I was wondering if this could be done with a proxy of some sort. Im still learning so i’m not 100% sure on the OOP strategies here.
class Parent
{
public $test;
public function boot()
{
// boot stuff
$this->internalStuff();
}
public function internalStuff()
{
$this->test = 'World!';
$c = new Child();
$c->cTest();
}
}
$p = new Parent;
$p->boot();
class Child extends Parent
{
public function __construct()
{}
public function cTest()
{
echo 'Hello ' . $this->test;
}
}
Inheritance (parent/child relationships between classes) only extends to the class definition. I.e. what methods and properties a class has. It only defines the “blueprint” of a class. The actual values are assigned to object instances of the class. You cannot modify the inheritance of an instantiated object. Neither does one object magically take over the values of another object, regardless of how their parent/child relationship is.
So no, it just doesn’t work that way. If you want a
Childobject to have certain values, you need to assign them to an instance of that object. Whatever you did with a completely different object instance of another class doesn’t matter.