3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Kitchen { private $yummy = 'cake'; } class MapperX { public function map(Kitchen $kitchen, array $data) { $mapper = $this; $map = function (Kitchen $kitchen, array $data) use ($mapper) { $mapper->setYummy($kitchen, $data); }; $map = Closure::bind($map, null, $kitchen); $map($kitchen, $data); } public function setYummy(Kitchen $kitchen, array $data) { $kitchen->yummy = $data['yummy']; } } class Mapper { public function setYummy(Kitchen $kitchen, array $data) { return function (Kitchen $kitchen) { $kitchen->yummy = $data['yummy']; }; } } $kitchen = new Kitchen; $mapper = new Mapper; $clj = $mapper->setYummy($kitchen, ['yummy' => 'cookie']); $clj->bindTo($kitchen); $clj(); var_dump($kitchen); /* $sweetsThief = function (Kitchen $kitchen) { return $kitchen->yummy; }; $sweetsCop = function (Kitchen $kitchen, $yummy) { $kitchen->yummy = $yummy; }; $kitchen = new Kitchen(); $sweetsThief = Closure::bind($sweetsThief, null, $kitchen); $sweetsCop = Closure::bind($sweetsCop, null, $kitchen); var_dump($sweetsThief($kitchen)); $sweetsCop($kitchen, 'lie'); var_dump($sweetsThief($kitchen)); */
Output for 7.1.0 - 7.1.25, 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.27, 8.2.0 - 8.2.17, 8.3.0 - 8.3.4
Fatal error: Uncaught ArgumentCountError: Too few arguments to function Mapper::{closure}(), 0 passed in /in/1DmUZ on line 43 and exactly 1 expected in /in/1DmUZ:32 Stack trace: #0 /in/1DmUZ(43): Mapper->{closure}() #1 {main} thrown in /in/1DmUZ on line 32
Process exited with code 255.
Output for 7.0.0 - 7.0.20
Fatal error: Uncaught TypeError: Argument 1 passed to Mapper::{closure}() must be an instance of Kitchen, none given, called in /in/1DmUZ on line 43 and defined in /in/1DmUZ:32 Stack trace: #0 /in/1DmUZ(43): Mapper->{closure}() #1 {main} thrown in /in/1DmUZ on line 32
Process exited with code 255.
Output for 5.4.2 - 5.4.45, 5.5.24 - 5.5.35, 5.6.7 - 5.6.28
Catchable fatal error: Argument 1 passed to Mapper::{closure}() must be an instance of Kitchen, none given, called in /in/1DmUZ on line 43 and defined in /in/1DmUZ on line 32
Process exited with code 255.

preferences:
170.17 ms | 402 KiB | 238 Q