3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Test { private $a='data'; protected $b=123; public $c=true; } /** * @author Lisachenko Alexander <lisachenko.it@gmail.com> */ class Hydrator { private function getContext() { return get_object_vars($this); } public function __invoke($object) { static $hydrator = null; if (!$hydrator) { $hydrator = (new ReflectionMethod(__CLASS__, 'getContext'))->getClosure($this); } return $hydrator->bindTo($object, get_class($object))->__invoke($object); } } $test = new Test; $hydrator = new Hydrator(); $data = $hydrator($test); var_dump($data);
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Warning: Cannot bind method Hydrator::getContext() to object of class Test in /in/BtTRD on line 26 Fatal error: Uncaught Error: Call to a member function __invoke() on null in /in/BtTRD:26 Stack trace: #0 /in/BtTRD(32): Hydrator->__invoke(Object(Test)) #1 {main} thrown in /in/BtTRD on line 26
Process exited with code 255.
Output for 7.0.0 - 7.0.33
Warning: Cannot rebind scope of closure created by ReflectionFunctionAbstract::getClosure() in /in/BtTRD on line 26 Fatal error: Uncaught Error: Call to a member function __invoke() on null in /in/BtTRD:26 Stack trace: #0 /in/BtTRD(32): Hydrator->__invoke(Object(Test)) #1 {main} thrown in /in/BtTRD on line 26
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
array(3) { ["a"]=> string(4) "data" ["b"]=> int(123) ["c"]=> bool(true) }
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /in/BtTRD on line 24
Process exited with code 255.

preferences:
254.37 ms | 401 KiB | 399 Q