3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?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();
based on q1vlU
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.4.11 - 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.
Output for 5.3.21 - 5.3.29
Parse error: syntax error, unexpected T_EVAL, expecting T_STRING in /in/rAd8I on line 10
Process exited with code 255.