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(function () use ($that) { $that->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 mZTMO
Output for 7.0.0 - 7.2.0
f, f Notice: Undefined variable: that in /in/cu3gc on line 86 > cause 1 * cleanup... f, t Notice: Undefined variable: that in /in/cu3gc on line 86 > cause 1 * cleanup... t, f Notice: Undefined variable: that in /in/cu3gc on line 86 > * cleanup... t, t Notice: Undefined variable: that in /in/cu3gc on line 86 Fatal error: Uncaught Error: Call to a member function reallyDoSomething() on null in /in/cu3gc:87 Stack trace: #0 /in/cu3gc(79): X->{closure}() #1 /in/cu3gc(88): X->check(Object(Closure)) #2 /in/cu3gc(119): X->doSomething() #3 {main} thrown in /in/cu3gc on line 87
Process exited with code 255.
Output for 5.6.8 - 5.6.28
f, f Notice: Undefined variable: that in /in/cu3gc on line 86 > cause 1 * cleanup... f, t Notice: Undefined variable: that in /in/cu3gc on line 86 > cause 1 * cleanup... t, f Notice: Undefined variable: that in /in/cu3gc on line 86 > * cleanup... t, t Notice: Undefined variable: that in /in/cu3gc on line 86 Fatal error: Call to a member function reallyDoSomething() on null in /in/cu3gc on line 87
Process exited with code 255.
Output for 5.3.21 - 5.5.35
f, f Notice: Undefined variable: that in /in/cu3gc on line 86 > cause 1 * cleanup... f, t Notice: Undefined variable: that in /in/cu3gc on line 86 > cause 1 * cleanup... t, f Notice: Undefined variable: that in /in/cu3gc on line 86 > * cleanup... t, t Notice: Undefined variable: that in /in/cu3gc on line 86 Fatal error: Call to a member function reallyDoSomething() on a non-object in /in/cu3gc on line 87
Process exited with code 255.