3v4l.org

run code in 300+ PHP versions simultaneously
<?php /** * PHP 5.6 example of "Functional Options" as presented by Rob Pike * in his article "Self-referential functions and the design of options" * http://commandcenter.blogspot.com.au/2014/01/self-referential-functions-and-design.html */ namespace Mailer { class Mailer { private $host; private $lazy = false; public function __construct($host, ...$options) { $this->host = $host; foreach ($options as $option) { $option($this); } } // Wish this could be a private function and still be // invoked by an options function so it's not mutable from the outside public function setLazy($lazy) { $this->lazy = $lazy; } public function isLazy() { return $this->lazy; } } } namespace Mailer\Options { class Lazy { public function __invoke(Mailer $mailer) { $mailer->setLazy(true); } } function SuperLazy() { return function(\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; } } namespace { use function \Mailer\Options\SuperLazy; $tls = function (\Mailer\Mailer $mailer) { echo 'LOL Lets do some TLS --'; }; // Any one of these $lazy vars can be passed as an option $lazy1 = function (\Mailer\Mailer $mailer) { $mailer->setLazy(true); }; $lazy2 = new \Mailer\Options\Lazy; $lazy3 = SuperLazy(); // Ughhh, I want to do `$lazy3 = SuperLazy;` but PHP won't let me $mailer = new \Mailer\Mailer('www.gmail.com', $tls, $lazy3); echo $mailer->isLazy() ? 'Yes lazy' : 'No lazy'; }
Output for 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
LOL Lets do some TLS --Yes lazy
Output for 5.4.0 - 5.4.45, 5.5.24 - 5.5.35
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/tTreF on line 14
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '.', expecting '&' or T_VARIABLE in /in/tTreF on line 14
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/tTreF on line 9
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/tTreF on line 9
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/tTreF on line 9
Process exited with code 255.

preferences:
150.01 ms | 401 KiB | 344 Q