3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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)); */
based on YvAlM
Output for 7.1.0 - 7.2.0
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.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.
Output for 5.3.18 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/1DmUZ on line 41
Process exited with code 255.