3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 }
based on OGqFu
Output for 7.0.0 - 7.3.0beta1
Warning: call_user_func() expects parameter 1 to be a valid callback, class 'strtoupper' not found in /in/G5fdt on line 54 Warning: call_user_func() expects parameter 1 to be a valid callback, class 'strtoupper' not found in /in/G5fdt on line 54
Output for 5.5.0 - 5.6.28
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/G5fdt on line 14
Process exited with code 255.