My Chart class actually allows me to create simple properties (of type string, boolean and so on) as well as nested object properties calling the magic __call method this way:
$chart = new Chart();
$chart->simple = 'Hello';
$chart->newComplex();
var_dump($chart);
Output:
object(Chart)[1]
public 'simple' => string 'Hello' (length=5)
public 'complex' =>
object(stdClass)[2]
I’d like to add the ability to create also nested object properties as children of other properties (not children of chart itself) in a way like this:
$chart->newComplex2($chart->newComplex1());
Question is: how to use $args parameter and modify __call() to accomplish this?
class Chart
{
public function __call($name, $args)
{
$type = substr($name, 0, 3);
$field = lcfirst(substr($name, strlen($type)));
switch($type)
{
case 'get': return isset($this->$field) ? $this->$field : null;
case 'new': return $this->$field = new stdClass();
}
}
}
I found solution by myself. The trick is to pass the parent property to
__call. Here is the code: