3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?php namespace Test; class Expression { private $closure; public function getClosure() { return $this->closure; } public function setClosure(\Closure $closure) { $this->closure = $closure; } public function getReflection() { return new \ReflectionFunction($this->getClosure()); } private function getLines() { $reflection = $this->getReflection(); $startLine = $reflection->getStartLine(); $endLine = $reflection->getEndLine(); return array_slice(file($reflection->getFileName()), $startLine - 1, ($endLine - $startLine) + 1); } private function getAllTokens(array $lines) { $code = sprintf('<?php %s ;', implode($lines)); return array_map(function ($token) { if (is_array($token)) { list($id, $value) = $token; return new Token($id, $value); } return new Token(T_SYMBOL, $token); }, array_filter(token_get_all($code), function ($token) { if (is_array($token)) { switch ($token[0]) { case T_INLINE_HTML: case T_BAD_CHARACTER: case T_COMMENT: case T_WHITESPACE: return false; } } return true; })); } private function getFilteredTokens(array $tokens) { while (!empty($tokens)) { $token = array_shift($tokens); if ($token->getId() == T_FUNCTION) { $depth = 0; $filteredTokens = []; foreach ($tokens as $token) { $filteredTokens[] = $token; if ($token->getId() == T_SYMBOL) { switch ($token->getValue()) { case '{': $depth += 1; break; case '}': $depth -= 1; if ($depth == 0) { return $filteredTokens; } break; } } } } } return []; } public function test() { $lines = $this->getLines(); $tokens = $this->getAllTokens($lines); $filteredTokens = $this->getFilteredTokens($tokens); return $filteredTokens; } public function __construct(\Closure $closure) { $this->setClosure($closure); } } const T_SYMBOL = 1099; class Token { private $id; private $value; public function getId() { return $this->id; } private function setId($id) { $this->id = (int) $id; } public function getValue() { return $this->value; } private function setValue($value) { $this->value = (string) $value; } public function getName() { if ($this->id != 1099) { return (string) token_name($this->id); } return 'T_SYMBOL'; } public function __construct($id, $value) { $this->setId($id); $this->setValue($value); } public function __toString() { return vsprintf('%-30s %s', [ $this->getName(), $this->getValue(), ]); } } $id = 1; $password = 'foobar'; $expression = new Expression(function (User $u) use ($id, $password) { return $u->id == $id && $u->password == $password; }); foreach ($expression->test() as $token) { echo $token . PHP_EOL; } var_dump($expression->getReflection()->getStaticVariables());
Output for 7.0.0 - 7.1.0
Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 Notice: Use of undefined constant T_BAD_CHARACTER - assumed 'T_BAD_CHARACTER' in /in/TDvqH on line 49 T_SYMBOL ( T_STRING User T_VARIABLE $u T_SYMBOL ) T_USE use T_SYMBOL ( T_VARIABLE $id T_SYMBOL , T_VARIABLE $password T_SYMBOL ) T_SYMBOL { T_RETURN return T_VARIABLE $u T_OBJECT_OPERATOR -> T_STRING id T_IS_EQUAL == T_VARIABLE $id T_BOOLEAN_AND && T_VARIABLE $u T_OBJECT_OPERATOR -> T_STRING password T_IS_EQUAL == T_VARIABLE $password T_SYMBOL ; T_SYMBOL } array(2) { ["id"]=> int(1) ["password"]=> string(6) "foobar" }
Output for 5.4.0 - 5.6.28, hhvm-3.10.0 - 3.12.0
T_SYMBOL ( T_STRING User T_VARIABLE $u T_SYMBOL ) T_USE use T_SYMBOL ( T_VARIABLE $id T_SYMBOL , T_VARIABLE $password T_SYMBOL ) T_SYMBOL { T_RETURN return T_VARIABLE $u T_OBJECT_OPERATOR -> T_STRING id T_IS_EQUAL == T_VARIABLE $id T_BOOLEAN_AND && T_VARIABLE $u T_OBJECT_OPERATOR -> T_STRING password T_IS_EQUAL == T_VARIABLE $password T_SYMBOL ; T_SYMBOL } array(2) { ["id"]=> int(1) ["password"]=> string(6) "foobar" }
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/TDvqH on line 67
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.2.17
Parse error: syntax error, unexpected T_STRING in /in/TDvqH on line 3
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_STRING in /in/TDvqH on line 3
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/TDvqH on line 3
Process exited with code 255.