3v4l.org

run code in 300+ PHP versions simultaneously
<?php class TestClass { } $abc = "testABC"; $xyz = new TestClass(); $test = true; $x = function () use ($test, $xyz, $abc) { echo $abc; var_dump($test, $xyz); var_dump($this); }; $x->bindTo(new TestClass, "TestClass"); $x(); echo packAnonFunction($x, $test, $xyz, $abc); function packAnonFunction($payload, ...$args) { $func = new ReflectionFunction($payload); $filename = $func->getFileName(); $start_line = $func->getStartLine() - 1; $end_line = $func->getEndLine(); $length = $end_line - $start_line; var_dump($func->getClosureThis()); $source = file($filename); $body = implode("", array_slice($source, $start_line, $length)); $body = preg_replace('/(?:(?:(\$[a-z]+)\s\=)|return)\sfunction/', '\\$payload = function', $body); if(preg_match('/use\s\((\$[a-zA-Z0-9]+(?:,\s\$[a-zA-Z0-9]+)*)\)/', $body, $matches)) { $vars = $matches[1]; if(strpos($vars, ', ') !== false) { $parts = explode(', ', $vars); } else { $parts = [$vars]; } $body = str_replace(" use (" . $vars . ")", "", $body); } else { $parts = []; } if(strpos($body, '$this') !== false) { $parts[] = '$this'; $args[count($parts) - 1] = $func->getClosureThis(); } $return = []; foreach($parts as $key => $variable) { $return[$variable] = $args[$key]; } $variableString = ""; foreach($return as $var => $value) { $value = serialize($value); $variableString .= "\t{$var} = unserialize('{$value}');\n"; } $body = str_replace("{\n", "{\n" . $variableString, $body); return $body; }
Output for git.master, git.master_jit, rfc.property-hooks
testABCbool(true) object(TestClass)#1 (0) { } Fatal error: Uncaught Error: Using $this when not in object context in /in/0RroH:11 Stack trace: #0 /in/0RroH(15): {closure}() #1 {main} thrown in /in/0RroH on line 11
Process exited with code 255.

This tab shows result from various feature-branches currently under review by the php developers. Contact me to have additional branches featured.

Active branches

Archived branches

Once feature-branches are merged or declined, they are no longer available. Their functionality (when merged) can be viewed from the main output page


preferences:
60.35 ms | 401 KiB | 8 Q