Added anonymous functions after php5.3, such as a function definition {code ...} for example, if Javascript can directly output the definition code of this function body, can I use echo $ test to output its function body elsewhere? Added anonymous functions after php 5.3, such as a function definition
$test = function () { echo 'hello world';}
For example, if I can directly output the definition code of this function body in js, can I use a similarecho $testMethod to output its function body?
Reply content:
Added anonymous functions after php 5.3, such as a function definition
$test = function () { echo 'hello world';}
For example, if I can directly output the definition code of this function body in js, can I use a similarecho $testMethod to output its function body?
Reflection can be implemented, of course @ Rodin also said, I simply implemented the code below
getMessage();return;}$start = $func->getStartLine() - 1;$end = $func->getEndLine() - 1;$filename = $func->getFileName();echo implode("", array_slice(file($filename),$start, $end - $start + 1));}closure_dump($test);
Anonymous functions are only available in PHP 5.3.0 and later versions.
To be honest, I have never used it before.
Can var_dump be used?
No. To call an anonymous function, use $ _ GLOBALS [].
$ GLOBALS ['test'] = function (){
Echo 'hello, world ';
};
$ Test ();
In this way, you can call
No. PHP does not have any interface to reflect its own code.
However, you can use the ReflectionFunction class to reflect a method to define the number of lines starting from the file to which the function code is read in conjunction with the file API.
For details, see:
Http://cn2.php.net/manual/en/class.re...