3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Condition { public function __constructor($at, $next = null) { $this->at = $at; $this->next = $next; } public function eval() { $result = $this->actualEval(); if ($result !== false && $this->next) $result = $this->next->eval(); return $result; } public function actualEval() { return false; } public function getCause() { return $this->cause ?: ($this->prev ? $this->prev->getCause() : null); } } class Condition1 extends Condition { public function __constructor($at, $next = null) { parent::__constructor($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->condition1; } } class X { protected function check(\Closure $callback) { $condition = new Condition1( $this, new Condition2($this) ); if (!$condition->eval()) { $this->log($condition->getCause()); $this->cleanup(); } else $callback(); } public function doSomething() { $this->check(function () use ($this) { $this->reallyDoSomething(); }); } public function reallyDoSomething() { echo "1"; } public function cleanup() { echo "> cleanup.\n"; } public function log($str) { echo "> $str\n"; } } $x = new X(); $x->doSomething();
Output for 7.0.0 - 7.0.2
Fatal error: Cannot use $this as lexical variable in /in/rAd8I on line 69
Process exited with code 255.
Output for 5.5.24 - 5.5.31, 5.6.8 - 5.6.17
Parse error: syntax error, unexpected 'eval' (T_EVAL), expecting identifier (T_STRING) in /in/rAd8I on line 10
Process exited with code 255.

preferences:
165.84 ms | 1395 KiB | 28 Q