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]+)\ \=\ function/', '\\$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 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.27, 8.2.0 - 8.2.18, 8.3.0 - 8.3.4, 8.3.6
testABCbool(true) object(TestClass)#1 (0) { } Fatal error: Uncaught Error: Using $this when not in object context in /in/ahYe2:11 Stack trace: #0 /in/ahYe2(15): {closure}() #1 {main} thrown in /in/ahYe2 on line 11
Process exited with code 255.
Output for 8.3.5
Warning: PHP Startup: Unable to load dynamic library 'sodium.so' (tried: /usr/lib/php/8.3.5/modules/sodium.so (libsodium.so.23: cannot open shared object file: No such file or directory), /usr/lib/php/8.3.5/modules/sodium.so.so (/usr/lib/php/8.3.5/modules/sodium.so.so: cannot open shared object file: No such file or directory)) in Unknown on line 0 testABCbool(true) object(TestClass)#1 (0) { } Fatal error: Uncaught Error: Using $this when not in object context in /in/ahYe2:11 Stack trace: #0 /in/ahYe2(15): {closure}() #1 {main} thrown in /in/ahYe2 on line 11
Process exited with code 255.
Output for 5.6.0 - 5.6.40, 7.0.0 - 7.0.33
testABCbool(true) object(TestClass)#1 (0) { } Notice: Undefined variable: this in /in/ahYe2 on line 11 NULL NULL $payload = function () { $test = unserialize('b:1;'); $xyz = unserialize('O:9:"TestClass":0:{}'); $abc = unserialize('s:7:"testABC";'); $this = unserialize('N;'); echo $abc; var_dump($test, $xyz); var_dump($this); };
Output for 5.5.0 - 5.5.38
Parse error: syntax error, unexpected '.', expecting '&' or variable (T_VARIABLE) in /in/ahYe2 on line 18
Process exited with code 255.

preferences:
255.73 ms | 401 KiB | 329 Q