3v4l.org

run code in 300+ PHP versions simultaneously
<?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();
Output for 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
f, f Warning: Undefined variable $that in /in/cu3gc on line 86 Deprecated: Creation of dynamic property Condition2::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition2::$next is deprecated in /in/cu3gc on line 10 Deprecated: Creation of dynamic property Condition1::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition1::$next is deprecated in /in/cu3gc on line 10 > cause 1 * cleanup... f, t Warning: Undefined variable $that in /in/cu3gc on line 86 Deprecated: Creation of dynamic property Condition2::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition2::$next is deprecated in /in/cu3gc on line 10 Deprecated: Creation of dynamic property Condition1::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition1::$next is deprecated in /in/cu3gc on line 10 > cause 1 * cleanup... t, f Warning: Undefined variable $that in /in/cu3gc on line 86 Deprecated: Creation of dynamic property Condition2::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition2::$next is deprecated in /in/cu3gc on line 10 Deprecated: Creation of dynamic property Condition1::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition1::$next is deprecated in /in/cu3gc on line 10 > * cleanup... t, t Warning: Undefined variable $that in /in/cu3gc on line 86 Deprecated: Creation of dynamic property Condition2::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition2::$next is deprecated in /in/cu3gc on line 10 Deprecated: Creation of dynamic property Condition1::$at is deprecated in /in/cu3gc on line 9 Deprecated: Creation of dynamic property Condition1::$next is deprecated in /in/cu3gc on line 10 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(86): 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 8.0.0 - 8.0.30, 8.1.0 - 8.1.28
f, f Warning: Undefined variable $that in /in/cu3gc on line 86 > cause 1 * cleanup... f, t Warning: Undefined variable $that in /in/cu3gc on line 86 > cause 1 * cleanup... t, f Warning: Undefined variable $that in /in/cu3gc on line 86 > * cleanup... t, t Warning: 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 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.0 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
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 7.3.32 - 7.3.33
f, f > cause 1 * cleanup... f, t > cause 1 * cleanup... t, f > * cleanup... t, t 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.5.24 - 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.

preferences:
157.32 ms | 402 KiB | 183 Q