How does php output code for an anonymous function?

Source: Internet
Author: User
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...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.