3v4l.org

run code in 300+ PHP versions simultaneously
<?php $obj = new stdClass(); $obj->name = 'Fred'; $obj->job = new stdClass(); $obj->job->position = 'Janitor'; $obj->job->years = 4; print_r($obj); echo 'Years in current job: '.string($obj, 'job->years').PHP_EOL; function string($obj, $path_str) { $val = null; $path = preg_split('/(\.|->)/', $path_str); $node = $obj; while (($prop = array_shift($path)) !== null) { if (!is_object($obj) || !property_exists($node, $prop)) { $val = null; break; } $val = $node->$prop; // TODO: Insert any logic here for cleaning up $val $node = $node->$prop; } return $val; }

preferences:
63.8 ms | 402 KiB | 5 Q