3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Water { public $color = 'clear'; public $temp = 'cold'; } class Pipeline { /** * @var array The parameters to pass through */ protected $parameters = []; /** * @var \Closure[] The list of pipes that the parameters will fall through * Pipe should match function($parameter..., \Closure $next) {} */ protected $pipes = []; /** * @type \Closure The "then" closure, it will receive the parameters you pipe through */ protected $thenClosure; /** * @inheritdoc */ public function send(...$parameters) : PipelineMap { $this->parameters = $parameters; return $this; } /** * @inheritdoc */ public function through($pipes) : PipelineMap { $this->pipes = is_array($pipes) ? $pipes : func_get_args(); return $this; } /** * @inheritdoc */ public function then(callable $then) : PipelineMap { $this->thenClosure = $then; return $this; } /** * @inheritdoc */ public function execute() { $pipes = array_reverse($this->pipes); /** @type \Closure $linked_closure */ $linked_closure = array_reduce($pipes, $this->getIterator(), $this->getInitial($this->thenClosure)); return $linked_closure(...$this->parameters); } /** * Get the iterator closure, this wraps every item in the list and injects the * @return \Closure */ protected function getIterator() : \Closure { return function ($next, $pipe) { return function (...$parameters) use ($next, $pipe) { $parameters[] = $next; return $pipe(...$parameters); }; }; } /** * The initial closure * @return \Closure */ protected function getInitial(\Closure $then) : \Closure { return function(...$parameters) use ($then) { return $then(...$parameters); }; } } // Lets make some pipes $color_water_blue_pipe = function (Water $water, callable $next_pipe) { $water->color = 'blue'; // We colored it, now we're done. Send it to the next pipe and return the result! return $next_pipe($water); }; $heat_water_pipe = function (Water $water, callable $next_pipe) { $water->temp = 'hot'; // We heated it, send it to the next pipe! return $next_pipe($water); }; // Make some water $water = $factory->getWater(); // Send it through our pipes $piped_water = (new \Buttress\Pipeline\Pipeline()) ->send($water) ->through([ $color_water_blue_pipe, $heat_water_pipe ]) ->then(function($water) { return $water; }); echo "The water is {$piped_water->color} and {$piped_water->temp}!";
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
Warning: Undefined variable $factory in /in/rEGaB on line 98 Fatal error: Uncaught Error: Call to a member function getWater() on null in /in/rEGaB:98 Stack trace: #0 {main} thrown in /in/rEGaB on line 98
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 Warning: Undefined variable $factory in /in/rEGaB on line 98 Fatal error: Uncaught Error: Call to a member function getWater() on null in /in/rEGaB:98 Stack trace: #0 {main} thrown in /in/rEGaB on line 98
Process exited with code 255.
Output for 7.0.5 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.33
Notice: Undefined variable: factory in /in/rEGaB on line 98 Fatal error: Uncaught Error: Call to a member function getWater() on null in /in/rEGaB:98 Stack trace: #0 {main} thrown in /in/rEGaB on line 98
Process exited with code 255.
Output for 7.3.32 - 7.3.33
Fatal error: Uncaught Error: Call to a member function getWater() on null in /in/rEGaB:98 Stack trace: #0 {main} thrown in /in/rEGaB on line 98
Process exited with code 255.
Output for 7.0.0 - 7.0.4
Notice: Undefined variable: factory in /in/rEGaB on line 98 Fatal error: Uncaught Error: Call to a member function getWater() on unknown in /in/rEGaB:98 Stack trace: #0 {main} thrown in /in/rEGaB on line 98
Process exited with code 255.
Output for 5.6.8 - 5.6.28
Parse error: syntax error, unexpected ':', expecting ';' or '{' in /in/rEGaB on line 26
Process exited with code 255.
Output for 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/rEGaB on line 26
Process exited with code 255.

preferences:
177.16 ms | 401 KiB | 213 Q