Hi! Could we please enable some services and cookies to improve your experience and our website?
Online Sandbox for SQL and PHP: Write, Run, Test, and Share SQL Queries and PHP Code
<?php
class Test
{
public $c;
function test() {
$a = 1;
$b = 2;
$this->c = $a + $b;
return $this->c;
}
public function test2(Closure $closure)
{
$func = new ReflectionFunction($closure);
$filename = $func->getFileName();
$start_line = $func->getStartLine() - 1; // it's actually - 1, otherwise you wont get the function() block
$end_line = $func->getEndLine();
$length = $end_line - $start_line;
$source = file($filename);
$ar = array_slice($source, $start_line, $length);
array_pop($ar);
array_shift($ar);
foreach ($ar as $a) {
list($var, $expression) = explode('=', $a);
$var1 = eval(trim($expression, '\n\s;'));
var_dump($var, $var1, $expression);
}
$body = implode("", array_slice($source, $start_line, $length));
print_r($body);
}
}
$t = new Test();
$tt = $t->test2(function () {
$a = 1;
$b = 2;
return $a + $b;
});