Created
November 1, 2019 18:55
-
-
Save AntonioCS/73da82dee74b0fd3430e6c8d7b3680bb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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