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)) { return $this->callbacks = $callbacks; } $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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, class "strtoupper" not found in /in/OGqFu:53 Stack trace: #0 /in/OGqFu(53): call_user_func(Array, 'hello') #1 [internal function]: Bar\PipeInterface@anonymous->Bar\{closure}('hello', Array) #2 /in/OGqFu(52): array_reduce(Array, Object(Closure), 'hello') #3 /in/OGqFu(59): Bar\PipeInterface@anonymous->process('hello') #4 /in/OGqFu(88): Bar\PipeInterface@anonymous->__invoke('hello') #5 {main} thrown in /in/OGqFu on line 53
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 Fatal error: Uncaught TypeError: call_user_func(): Argument #1 ($callback) must be a valid callback, class "strtoupper" not found in /in/OGqFu:53 Stack trace: #0 /in/OGqFu(53): call_user_func(Array, 'hello') #1 [internal function]: Bar\PipeInterface@anonymous->Bar\{closure}('hello', Array) #2 /in/OGqFu(52): array_reduce(Array, Object(Closure), 'hello') #3 /in/OGqFu(59): Bar\PipeInterface@anonymous->process('hello') #4 /in/OGqFu(88): Bar\PipeInterface@anonymous->__invoke('hello') #5 {main} thrown in /in/OGqFu on line 53
Process exited with code 255.
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.33, 7.4.0 - 7.4.33
Warning: call_user_func() expects parameter 1 to be a valid callback, class 'strtoupper' not found in /in/OGqFu on line 53 Warning: call_user_func() expects parameter 1 to be a valid callback, class 'strtoupper' not found in /in/OGqFu on line 53
Output for 5.5.0 - 5.5.37, 5.6.0 - 5.6.28
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/OGqFu on line 14
Process exited with code 255.

preferences:
208.57 ms | 402 KiB | 220 Q