@ 2015-12-05T21:55:56Z <?php
class TestClass {
function getClosure() {
return function () {
var_dump($this);
}
}
}
$abc = "testABC";
$xyz = new TestClass();
$test = true;
$x = function () use ($test, $xyz, $abc) {
echo $abc;
var_dump($test, $xyz);
var_dump($this);
};
$test = new TestClass();
echo packAnonFunction($x, $test, $xyz, $abc);
echo packAnonFunction($test->getClosure());
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;
}
Enable javascript to submit You have javascript disabled. You will not be able to edit any code.
Here you find the average performance (time & memory) of each version. A grayed out version indicates it didn't complete successfully (based on exit-code).
Version System time (s) User time (s) Memory (MiB) 7.0.0 0.003 0.097 20.24 5.6.16 0.007 0.080 20.55 5.6.15 0.017 0.070 18.25 5.6.14 0.013 0.070 18.18 5.6.13 0.000 0.043 18.18 5.6.12 0.010 0.053 21.15 5.6.11 0.013 0.077 21.00 5.6.10 0.007 0.087 20.89 5.6.9 0.003 0.093 21.11 5.6.8 0.017 0.077 20.37 5.5.30 0.013 0.050 17.92 5.5.29 0.003 0.050 17.99 5.5.28 0.013 0.070 20.86 5.5.27 0.007 0.070 20.98 5.5.26 0.003 0.043 20.96 5.5.25 0.003 0.087 20.62 5.5.24 0.020 0.077 20.13
preferences:dark mode live preview
142.14 ms | 1394 KiB | 7 Q