3v4l.org

run code in 300+ PHP versions simultaneously
<?php class ObjectToArrayConverter { /** * @param mixed $value * * @raturn array */ public static function toArray($value) { if (! (is_object($value) || is_array($value))) { return $value; } if (is_object($value) && method_exists($value, '__toArray')) { return $value->__toArray(); } return array_map(__METHOD__, (array) $value); } } clas Baz { public function __toArray() { return ['I R ARRAY']; } } $obj1 = new stdClass(); $obj2 = new stdClass(); $obj1->foo = 'foo'; $obj1->someArr = []; $obj1->obj2 = $obj2; $obj2->bar = 'bar'; $obj2->baz = new Baz(); var_dump(ObjectToArrayConverter::toArray($obj1));
Output for 5.4.0 - 5.4.32, 5.5.0 - 5.5.16, 5.6.0 - 5.6.1
Parse error: syntax error, unexpected 'Baz' (T_STRING) in /in/QDv0V on line 24
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_STRING in /in/QDv0V on line 24
Process exited with code 255.

preferences:
227.69 ms | 1399 KiB | 89 Q