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);
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught TypeError: count(): Argument #1 ($value) must be of type Countable|array, string given in /in/7DE8b:55 Stack trace: #0 {main} thrown in /in/7DE8b on line 55
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
42.92 ms | 401 KiB | 8 Q