3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php class Condition { var $causedNext = false; var $cause = null; public function __construct($at, $next = null) { $this->at = $at; $this->next = $next; } public function evalCondition() { $this->causedNext = false; $result = $this->actualEval(); if ($result !== false && $this->next) { $result = $this->next->evalCondition(); if ($result === false) $this->causedNext = true; } return $result; } public function actualEval() { return false; } public function getCause() { return $this->causedNext ? $this->next->getCause() : $this->cause; } } class Condition1 extends Condition { public function __construct($at, $next = null) { parent::__construct($at, $next); $this->cause = "cause 1"; } public function actualEval() { return $this->at->condition1; } } class Condition2 extends Condition { public function __constructor($at, $next = null) { parent::__constructor($at, $next); $this->cause = "cause 2"; } public function actualEval() { return $this->at->condition2; } } class X { var $condition1 = false; var $condition2 = false; public function __construct($c1, $c2) { $this->condition1 = $c1; $this->condition2 = $c2; } protected function check(\Closure $callback) { $condition = new Condition1( $this, new Condition2($this) ); if (!$condition->evalCondition()) { $this->log($condition->getCause()); } else return $callback(); $this->cleanup(); return null; } public function doSomething() { $this->check($this->reallyDoSomething); } public function reallyDoSomething() { echo "! Done!"; } public function cleanup() { echo "* cleanup...\n"; } public function log($str) { echo "> $str\n"; } } echo "f, f\n"; $x = new X(false, false); $x->doSomething(); echo "f, t\n"; $x = new X(false, true); $x->doSomething(); echo "t, f\n"; $x = new X(true, false); $x->doSomething(); echo "t, t\n"; $x = new X(true, true); $x->doSomething();
based on 8T21V
Output for 7.0.0 - 7.2.0
f, f Notice: Undefined property: X::$reallyDoSomething in /in/mZTMO on line 86 Fatal error: Uncaught TypeError: Argument 1 passed to X::check() must be an instance of Closure, null given, called in /in/mZTMO on line 86 and defined in /in/mZTMO:70 Stack trace: #0 /in/mZTMO(86): X->check(NULL) #1 /in/mZTMO(105): X->doSomething() #2 {main} thrown in /in/mZTMO on line 70
Process exited with code 255.
Output for 5.3.21 - 5.6.28
f, f Notice: Undefined property: X::$reallyDoSomething in /in/mZTMO on line 86 Catchable fatal error: Argument 1 passed to X::check() must be an instance of Closure, null given, called in /in/mZTMO on line 86 and defined in /in/mZTMO on line 70
Process exited with code 255.