3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php class State { protected $foo; protected $bar; public function getFoo() { return $this->foo; } public function getBar() { return $this->bar; } public final function setFoo($foo) { $this->foo = $foo; } public final function setBar($bar) { $this->bar = $bar; } public final function transaction(callable $function) { $proxy = new StateProxy($this); if (call_user_func($function, $proxy)) { $proxy->commit(); } unset($proxy); } } class StateProxy extends State { protected $state; public function __construct(State $state) { $this->state = $state; } public function getFoo() { return parent::getFoo() ? : $this->state->getFoo(); } public function getBar() { return parent::getBar() ? : $this->state->getBar(); } public function commit() { $this->state->foo = $this->foo; $this->state->bar = $this->bar; } } $state = new State(); $state->setFoo('abc'); $state->setBar('123'); var_dump($state); $state->transaction(function(StateProxy $proxy) { $proxy->setFoo('def'); $proxy->setBar('456'); return true; }); var_dump($state); $state->transaction(function(StateProxy $proxy) { $proxy->setFoo('ghi'); $proxy->setBar('789'); return false; }); var_dump($state); $state = new State(); $state->setFoo('abc'); $state->setBar('123'); $state->transaction(function(StateProxy $proxy) { $proxy->transaction(function(StateProxy $proxy) { $proxy->transaction(function(StateProxy $proxy) { $proxy->transaction(function(StateProxy $proxy) { $proxy->setFoo('def'); $proxy->setBar('456'); return false; }); $proxy->transaction(function(StateProxy $proxy) { $proxy->setFoo('ghi'); $proxy->setBar('789'); return true; }); return true; }); $proxy->transaction(function(StateProxy $proxy) { $proxy->setFoo('jkl'); $proxy->setBar('012'); return false; }); return true; }); return true; }); var_dump($state);
Output for 5.4.0 - 5.6.25, hhvm-3.10.0, 7.0.0 - 7.1.0
object(State)#1 (2) { ["foo":protected]=> string(3) "abc" ["bar":protected]=> string(3) "123" } object(State)#1 (2) { ["foo":protected]=> string(3) "def" ["bar":protected]=> string(3) "456" } object(State)#1 (2) { ["foo":protected]=> string(3) "def" ["bar":protected]=> string(3) "456" } object(State)#2 (2) { ["foo":protected]=> string(3) "ghi" ["bar":protected]=> string(3) "789" }
Output for hhvm-3.12.0
object(State)#1 (2) { ["foo":protected]=> string(3) "abc" ["bar":protected]=> string(3) "123" } object(State)#1 (2) { ["foo":protected]=> string(3) "def" ["bar":protected]=> string(3) "456" } object(State)#1 (2) { ["foo":protected]=> string(3) "def" ["bar":protected]=> string(3) "456" } object(State)#4 (2) { ["foo":protected]=> string(3) "ghi" ["bar":protected]=> string(3) "789" }
Output for 5.3.0 - 5.3.29
object(State)#1 (2) { ["foo":protected]=> string(3) "abc" ["bar":protected]=> string(3) "123" } Catchable fatal error: Argument 1 passed to State::transaction() must be an instance of callable, instance of Closure given, called in /in/BfmpQ on line 66 and defined in /in/BfmpQ on line 24
Process exited with code 255.
Output for 5.1.0 - 5.2.17
Parse error: syntax error, unexpected ':' in /in/BfmpQ on line 43
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected ':' in /in/BfmpQ on line 43
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/BfmpQ on line 5
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/BfmpQ on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/BfmpQ on line 5
Process exited with code 255.