note 126578 added to function.serialize
| From: | synnus at main dot php dot net | Date: | Thu, 11 Nov 2021 19:57:08 +0000 |
| Subject: | note 126578 added to function.serialize | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-217189@lists.php.net to get a copy of this message | ||
https://stackoverflow.com/questions/22308365/get-string-representation-of-anonymous-function
https://3v4l.org/CEmqV
UPDATE PHP8
<?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);
}
$dog2 = function(){
return 'test';
};
echo closure_to_str($dog2)."\n\n";
?>
Return String
function(){
return 'test';
}
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.242 (proxied: 147.135.154.63)
----
Manual Page -- https://php.net/manual/en/function.serialize.php
Edit -- https://main.php.net/note/edit/126578
Del: integrated -- https://main.php.net/note/delete/126578/integrated
Del: useless -- https://main.php.net/note/delete/126578/useless
Del: bad code -- https://main.php.net/note/delete/126578/bad+code
Del: spam -- https://main.php.net/note/delete/126578/spam
Del: non-english -- https://main.php.net/note/delete/126578/non-english
Del: in docs -- https://main.php.net/note/delete/126578/in+docs
Del: other reasons-- https://main.php.net/note/delete/126578
Reject -- https://main.php.net/note/reject/126578
Search -- https://main.php.net/manage/user-notes.php