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]; // 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);
Output for 8.0.10 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /in/oEL6a:61 Stack trace: #0 /in/oEL6a(56): consume(Object(BasicModel), 'f') #1 {main} thrown in /in/oEL6a on line 61
Process exited with code 255.
Output for 8.0.0 - 8.0.9
Fatal error: Uncaught TypeError: count(): Argument #1 ($var) must be of type Countable|array, string given in /in/oEL6a:61 Stack trace: #0 /in/oEL6a(56): consume(Object(BasicModel), 'f') #1 {main} thrown in /in/oEL6a on line 61
Process exited with code 255.
Output for 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 61 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 62 Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 61 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 62 Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 61 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 62 Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 61 int(1) Warning: count(): Parameter must be an array or an object that implements Countable in /in/oEL6a on line 62 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.5.0 - 5.5.38, 5.6.0 - 5.6.40, 7.0.0 - 7.0.33, 7.1.0 - 7.1.33
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) } }

preferences:
271.58 ms | 403 KiB | 330 Q