3v4l.org

run code in 300+ PHP versions simultaneously
<?php namespace Bar { interface PipeInterface { public function __construct($callbacks); public function pipe(callable $fn); public function process($payload); } function map(callable $fn) { return new class($fn) { private $fn; public function __construct(callable $fn) { $this->fn = $fn; } public function __invoke($payload) { return array_map($this->fn, $payload); } }; } function pipe($callbacks) { return new class($callbacks) implements PipeInterface { private $callbacks; public function __construct($callbacks) { if (is_array($this->callbacks)) { $this->callbacks = $callbacks; return; } $this->callbacks = [$callbacks]; } public function pipe(callable $callback) { return new self(array_merge($this->callbacks, [$callback])); } public function process($payload) { return array_reduce($this->callbacks, function ($payload, $callback) { return call_user_func($callback, $payload); }, $payload); } public function __invoke($payload) { return $this->process($payload); } }; } } namespace Foo { use function Bar\pipe; use function Bar\map; $xs = [ [ 'name' => 'Sérgio' ], [ 'name' => 'Marcelo' ] ]; $pipe = pipe(map(function ($x) { return $x['name']; })); // var_dump($pipe($xs)); // works $upper = pipe('strtoupper'); // echo $upper('hello'); // works $rev = $upper->pipe('strrev'); // doesn't work echo $rev('hello'); // doesn't work echo pipe(['strtoupper', 'strrev'])('hello'); // doesn't work }
Output for git.master, git.master_jit, rfc.property-hooks
Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, class "strtoupper" not found in /in/G5fdt:54 Stack trace: #0 /in/G5fdt(54): call_user_func(Array, 'hello') #1 [internal function]: Bar\PipeInterface@anonymous->Bar\{closure}('hello', Array) #2 /in/G5fdt(53): array_reduce(Array, Object(Closure), 'hello') #3 /in/G5fdt(60): Bar\PipeInterface@anonymous->process('hello') #4 /in/G5fdt(89): Bar\PipeInterface@anonymous->__invoke('hello') #5 {main} thrown in /in/G5fdt on line 54
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:
59.58 ms | 402 KiB | 8 Q