- file_get_contents: documentation ( source)
- substr_count: documentation ( source)
- array_slice: documentation ( source)
- implode: documentation ( source)
- explode: documentation ( source)
<?php
function closure_to_str($func)
{
$refl = new \ReflectionFunction($func); // get reflection object
$path = $refl->getFileName(); // absolute path of php file
$begn = $refl->getStartLine(); // have to `-1` for array index
$endn = $refl->getEndLine();
$dlim = PHP_EOL;
$list = explode($dlim, file_get_contents($path)); // lines of php-file source
$list = array_slice($list, ($begn-1), ($endn-($begn-1))); // lines of closure definition
$last = (count($list)-1); // last line number
if((substr_count($list[0],'function')>1)|| (substr_count($list[0],'{')>1) || (substr_count($list[$last],'}')>1))
{ throw new \Exception("Too complex context definition in: `$path`. Check lines: $begn & $endn."); }
$list[0] = ('function'.explode('function',$list[0])[1]);
$list[$last] = (explode('}',$list[$last])[0].'}');
return implode($dlim,$list);
}
$test = 10;
$dog2 = function($var=0) use ($test){
$var = 10;
echo $var . PHP_EOL;
return $test . $var;
};
echo closure_to_str($dog2)."\n\n";