This question already has an answer here:
I make another question cuz i think people doesn't understand my old question:
Why a protected variable of parent class come empty?
i.e:
class Father {
protected $body;
function __construct()
{
}
public function run()
{
$this->change();
$this->change2();
}
private function change()
{
$this->body = 'new value';
}
private function change2()
{
$this->body = str_replace('value','........','new value');
}
}
class Child extends Father {
function __construct()
{
echo $this->body;
}
}
$father = new Father();
$father->run();
$child = new Child();
body come empty, i need to continue changing this variable body inside Child class but it come empty, i think the unique solution would be to set body as static.
sry for asking a similar question but i'm trying to understand it.
Edit: if i put all these code of run method inside __construct and in child class call parent::__construct() it will make these changes again and i can't do it again cuz i need to continue changing it in child class.
</div>
I think that OP does not undarstand OOP.
Anyway you can reach this wanted behaviour with using static. protected $body;
replace with static protected $body;
and $this->body
with self::body
.