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) { print_r($node); echo 'Property exists: '.$prop; if (!property_exists($node, $prop)) { $val = null; break; } $val = $node->$prop; $node = $node->$prop; } return $val; }

preferences:
59.2 ms | 402 KiB | 5 Q