3v4l.org

run code in 300+ PHP versions simultaneously
<?php $properties = [ 'name' => ['f'], 'name2' => ['relation->g'], # one possibility 'name3' => ['relation', 'g'], # another possibility, (could just be an explode('->')) 'name4' => ['relation', ['format', 'd-m-Y']], # chaining a 2nd method call ]; class BasicModel { public $f = 123; public function relation() { return new RelatedModel; } public function __get($name) { if (method_exists($this, $name)) { return $this->$name(); } } public function __call($name, $arguments) { if (method_exists($this, $name)) { return call_user_func_array ([$this, $name], $arguments); } } } class RelatedModel extends BasicModel # extends to get __get, __call functionality { public $g = 567; public function format($format) { return $format; # tbi } } # here is implementation $model = new BasicModel; foreach ($properties as $name => $_) { // match $method = $_[0]; var_dump(count($method)); // call $data[$name] = consume($model, $method); # here, is there an easy way to support all above? or something very close? } function consume($model, $methods) { var_dump(count($methods)); if (count($methods) == 1) { if (strpos($methods[0], '->') !== false) { // there are '->', so explode and repeat return consume($model, explode('->', $methods)); # second case } return $model->$methods; # first case } else { var_dump('not one', $methods); $part = $model; foreach ($methods as $chainCall) { var_dump($part); $part = $part->$chainCall; } return $part; } } var_dump($data);

preferences:
44.62 ms | 402 KiB | 5 Q