3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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 getFoo() . PHP_EOL; $setFoo(1); //set new cost echo $obj->getTotal() . PHP_EOL; echo getFoo() . PHP_EOL;
based on qJkXC
Output for 7.0.0 - 7.2.0
6.5420560747664 Fatal error: Uncaught Error: Call to undefined function getFoo() in /in/jQIjb:31 Stack trace: #0 {main} thrown in /in/jQIjb on line 31
Process exited with code 255.
Output for 5.4.11 - 5.6.28
6.5420560747664 Fatal error: Call to undefined function getFoo() in /in/jQIjb on line 31
Process exited with code 255.
Output for 5.3.21 - 5.3.29
Fatal error: Call to undefined method Closure::bind() in /in/jQIjb on line 15
Process exited with code 255.