3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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);
based on oEL6a
Output for 7.2.0
Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 55 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 62 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 63 Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 55 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 62 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 63 Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 55 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 62 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 63 Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 55 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 62 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/7DE8b on line 63 array(4) { ["name"]=> int(123) ["name2"]=> NULL ["name3"]=> object(RelatedModel)#2 (2) { ["g"]=> int(567) ["f"]=> int(123) } ["name4"]=> object(RelatedModel)#3 (2) { ["g"]=> int(567) ["f"]=> int(123) } }
Output for 5.4.12 - 7.1.10
int(1) int(1) int(1) int(1) int(1) int(1) int(1) int(1) array(4) { ["name"]=> int(123) ["name2"]=> NULL ["name3"]=> object(RelatedModel)#2 (2) { ["g"]=> int(567) ["f"]=> int(123) } ["name4"]=> object(RelatedModel)#3 (2) { ["g"]=> int(567) ["f"]=> int(123) } }
Output for 5.3.22 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/7DE8b on line 3
Process exited with code 255.