- property_exists: documentation ( source)
- var_dump: documentation ( source)
- array_shift: documentation ( source)
- explode: documentation ( source)
<?php
class Foo
{
protected $bar;
public function __construct(Bar $bar)
{
$this->bar = $bar;
}
public function __get($key)
{
$exploded = explode('.', $key);
$item = $this;
do {
$next = array_shift($exploded);
if (!property_exists($item, $next)) {
return null;
}
$item = $item->{$next};
} while ($exploded);
return $item;
}
}
class Bar
{
public $foobar = 'test';
}
$foo = new Foo(new Bar);
var_dump($foo->{"bar.foobar"});