How can I access a deep object property named as a variable (dot notation) in php?

It is very easy to reduce the object path using variable property notation ($o->$p):

$path="foo.bar";
echo array_reduce(explode('.', $path), function ($o, $p) { return $o->$p; }, $user);

This could easily be turned into a small helper function.

Leave a Comment