<?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));
*/
- Output for 7.0.0 - 7.0.33, 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.18, 8.3.0 - 8.3.6
- 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.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
- Fatal error: Cannot access private property Kitchen::$yummy in /in/oK89U on line 24
Process exited with code 255.
preferences:
285.09 ms | 402 KiB | 376 Q