3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Kitchen { private $yummy = 'cake'; } class Mapper { 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']; } } $kitchen = new Kitchen; $mapper = new Mapper; $mapper->map($kitchen, ['yummy' => 'cookie']); 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 dr6og
Output for 7.0.0 - 7.2.0
Fatal error: Uncaught Error: Cannot access private property Kitchen::$yummy in /in/oK89U:24 Stack trace: #0 /in/oK89U(15): Mapper->setYummy(Object(Kitchen), Array) #1 /in/oK89U(19): Kitchen::{closure}(Object(Kitchen), Array) #2 /in/oK89U(30): Mapper->map(Object(Kitchen), Array) #3 {main} thrown in /in/oK89U on line 24
Process exited with code 255.
Output for 5.4.0 - 5.6.28
Fatal error: Cannot access private property Kitchen::$yummy in /in/oK89U on line 24
Process exited with code 255.