3v4l.org

run code in 300+ PHP versions simultaneously
<?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'));
Output for git.master, git.master_jit, rfc.property-hooks
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.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
52.36 ms | 401 KiB | 8 Q