10

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 string
  • var_dump($l); class Closure#1 (0) { }
  • echo $l->__toString();: Call to undefined method Closure::__toString()
  • get_class_methods($l) returns array('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.

user2864740
  • 60,010
  • 15
  • 145
  • 220
Michael Osl
  • 2,720
  • 1
  • 21
  • 36
  • What do you think about `create_function()` so that you can have the function content as string and just need to name it something through this function? – Javad Mar 10 '14 at 18:38
  • @Javad that's a good idea. However, I'd like to pass the anonymous function as a method parameter. Sure I could simply pass a string and the method's parameters instead and use `create_function()`, but then the syntax will be checked only at runtime, which kind of contradicts the idea of anonymous functions. – Michael Osl Mar 10 '14 at 18:48
  • Yes you are right, but if you check the manual for `create_function()` you can create an anonymous function by this [http://ca1.php.net/create_function] (Check first Example) – Javad Mar 10 '14 at 18:57
  • No. Closures are compiled at runtime like any other piece of code. The only way around this is to store the function body as a string and use `create_function()` as @Javad has already stated. – Sammitch Mar 10 '14 at 19:27

2 Answers2

4

Your only "real" option is to use an actual PHP parser to parse the anonymous function.

Don't fall in the same trap as @argon did thinking a simple substr_count based script can actually parse PHP code. It's just too brittle and will break even for the most simple examples in glorious ways.

Point in case:

$dog2 = ['bark'=>function($foo = '{'){}];

Fatal error: Uncaught Exception: Too complex context definition in

https://3v4l.org/CEmqV

user3942918
  • 25,539
  • 11
  • 55
  • 67
PeeHaa
  • 71,436
  • 58
  • 190
  • 262
  • 1
    No need for expensive PHP parsing; if the source-code is too messy then it should be neatened up for readability. .. and your example above is unfair, why don't you simply edit the answer? too complicated for you? –  Jul 21 '17 at 16:28
  • 1
    How is the example above in any way "messy"? – PeeHaa Jul 21 '17 at 16:31
  • see edited comment above @ fairness, look I can update the function to make it be "perfect" (for any closure - on one line) - but that would make it more complicated than necessary. –  Jul 21 '17 at 16:35
  • I've deleted my answer, since no one has the time to comment or edit but only down-vote; you should be ashamed of yourself. –  Jul 21 '17 at 16:43
-1

Its an oxymoron to access the source code of an anonymous file. If you need to refer to the source, then don't create an anonymous function:

$myfunction='echo "hello world";';
$l = create_function($myfunction);
print highlight_string($myfunction) . " results in....<br />";
$l();

(hint, you might want to have a look at func_get_args())

symcbean
  • 47,736
  • 6
  • 59
  • 94