3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Translator1 { private $translator2; public function __construct(Translator2 $t2) { $this->translator2 = $t2; } } class Translator2 { private $translator3; public function __construct(Translator3 $t3) { $this->translator3 = $t3; } } class Translator3 extends ParentTranslator { // none found, look for 'x' in parent class } class ParentTranslator { private $x = 'LOL'; public function __construct() { $this->x = 'LOL'; } } function accessPrivatePropertyOf($object, ...$properties) { if (empty($properties)) { return $object; } $reflector = new \ReflectionClass($object); $property = array_shift($properties); echo 'Looking in: ' . (is_object($object) ? get_class($object) : gettype($object)) . ' for property: ' . $property . PHP_EOL; if (!$reflector->hasProperty($property)) { /** Try parent class **/ $parent = $reflector->getParentClass(); echo 'BUT... does not exist, looking in parent: ' . $object->getName() . ' instead...' . PHP_EOL; if (!$parent->hasProperty($property)) { throw new \Exception; } $prop = $parent->getProperty($property); $prop->setAccessible(true); $property = $prop->getValue($object); //echo 'We found property with name: ' . $prop->getName() . ' and value: ' . $prop->getValue($object) . PHP_EOL; return accessPrivatePropertyOf($property, ...$properties); } $prop = $reflector->getProperty($property); $prop->setAccessible(true); $property = $prop->getValue($object); return accessPrivatePropertyOf($property, ...$properties); } $t = new Translator1(new Translator2(new Translator3)); // Shows no value for x WHYY?Y?? var_dump(accessPrivatePropertyOf($t, 'translator2', 'translator3', 'x'));
based on WkeGP
Output for 7.0.0 - 7.3.0beta1
Looking in: Translator1 for property: translator2 Looking in: Translator2 for property: translator3 Looking in: Translator3 for property: x Fatal error: Uncaught Error: Call to undefined method Translator3::getName() in /in/EjdNt:55 Stack trace: #0 /in/EjdNt(75): accessPrivatePropertyOf(Object(Translator3), 'x') #1 /in/EjdNt(75): accessPrivatePropertyOf(Object(Translator2), 'translator3', 'x') #2 /in/EjdNt(81): accessPrivatePropertyOf(Object(Translator1), 'translator2', 'translator3', 'x') #3 {main} thrown in /in/EjdNt on line 55
Process exited with code 255.
Output for 5.6.0 - 5.6.28
Looking in: Translator1 for property: translator2 Looking in: Translator2 for property: translator3 Looking in: Translator3 for property: x Fatal error: Call to undefined method Translator3::getName() in /in/EjdNt on line 55
Process exited with code 255.
Output for 5.5.0 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/EjdNt on line 37
Process exited with code 255.