<?php
class Item
{
private $cost = 100;
public function getTotal($discount = 7) {
return $this->cost * $discount/(100+$discount);
}
}
class Hydrator
{
public function getPrivate($obj, $attribute) {
$getter = function() use ($attribute) {return $this->$attribute;};
return \Closure::bind($getter, $obj, get_class($obj));
}
public function setPrivate($obj, $attribute) {
$setter = function($value) use ($attribute) {$this->$attribute = $value;};
return \Closure::bind($setter, $obj, get_class($obj));
}
}
$obj = new Item();
$hydrated = new Hydrator();
$getFoo = $hydrated->getPrivate($obj, 'cost');
$setFoo = $hydrated->setPrivate($obj, 'cost');
echo $obj->getTotal() .PHP_EOL;
$setFoo(1);
echo $obj->getTotal() .PHP_EOL;
- Output for 5.5.24 - 5.5.35, 5.6.8 - 5.6.28, 7.0.0 - 7.0.20, 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.28, 8.2.0 - 8.2.19, 8.3.0 - 8.3.7
- 6.5420560747664
0.065420560747664
preferences:
161.3 ms | 404 KiB | 199 Q