Let's say I have an anonymous PHP function like this:
<?php
$l = function() { echo "hello world"; };
Is it possible to get a string representation of the anonymous function $l
, i.e. a string containing the function body?
I tried a few things:
echo $l;
PHP Catchable fatal error: Object of class Closure could not be converted to stringvar_dump($l);
class Closure#1 (0) { }echo $l->__toString();
: Call to undefined method Closure::__toString()get_class_methods($l)
returnsarray('bind', 'bindTo')
, so it seems like no undocumented methods exist$r = new ReflectionClass($l);
: getProperties(), getConstants() and getStaticProperties() are all empty, also$r->__toString()
does not return anything useful.
I don't really need this in my code, I just thought it might be useful for logging purposes if something goes wrong. After I couldn't come up with a solution by myself I am curious if it is possible at all.