Skip to content

Instantly share code, notes, and snippets.

@AntonioCS
Created November 1, 2019 18:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AntonioCS/73da82dee74b0fd3430e6c8d7b3680bb to your computer and use it in GitHub Desktop.
Save AntonioCS/73da82dee74b0fd3430e6c8d7b3680bb to your computer and use it in GitHub Desktop.
<?php
class Pappi {
private $prop = "default value";
}
class Child extends Pappi {
public function setProp(string $value)
{
$this->setValueParentProperty('prop', $value);
}
public function getProp() : string
{
return $this->getValueParentProperty('prop');
}
private function getParentProperty(string $prop) : \ReflectionProperty
{
$r = new \ReflectionClass(parent::class);
$p = $r->getProperty($prop);
$p->setAccessible(true);
return $p;
}
private function getValueParentProperty(string $prop)
{
return $this->getParentProperty($prop)
->getValue($this);
}
private function setValueParentProperty(string $prop, $value) : void
{
$res = $this->getParentProperty($prop);
$res->setValue($this, $value);
}
}
$c = new Child();
var_dump($c->getProp());
$c->setProp('New value');
var_dump($c->getProp());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment