@ 2016-01-13T19:46:37Z <?php
class Item
{
private $cost = 100;
public function getTotal($discount = 7) {
return $this->cost * $discount/(100+$discount);
}
}
class AccesserToPrivated
{
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();
$accesser = new AccesserToPrivated();
$getFoo = $accesser->getPrivate($obj, 'cost');
$setFoo = $accesser->setPrivate($obj, 'cost');
echo $obj->getTotal() . PHP_EOL;
echo $obj->getFoo();
$setFoo(1);
echo $obj->getTotal() . PHP_EOL;
echo $obj->getFoo();
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
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 6.5420560747664
Fatal error: Uncaught Error: Call to undefined method Item::getFoo() in /in/qJkXC:31
Stack trace:
#0 {main}
thrown in /in/qJkXC on line 31
Process exited with code 255 . Output for 5.5.0 - 5.5.38 , 5.6.0 - 5.6.40 6.5420560747664
Fatal error: Call to undefined method Item::getFoo() in /in/qJkXC on line 31
Process exited with code 255 . preferences:dark mode live preview
201.92 ms | 402 KiB | 330 Q