php - Get string representation of anonymous function -


let's have anonymous php function this:

<?php $l = function() { echo "hello world"; }; 

is possible string representation of anonymous function $l, i.e. string containing function body?

i tried few things:

  • echo $l; php catchable fatal error: object of class closure not converted string
  • var_dump($l); class closure#1 (0) { }
  • echo $l->__tostring();: call undefined method closure::__tostring()
  • get_class_methods($l) returns array('bind', 'bindto'), seems no undocumented methods exist
  • $r = new reflectionclass($l);: getproperties(), getconstants() , getstaticproperties() empty, $r->__tostring() not return useful.

i don't need in code, thought might useful logging purposes if goes wrong. after couldn't come solution myself curious if possible @ all.

your "real" option use actual php parser parse anonymous function.

don't fall in the same trap @argon did thinking simple substr_count based script can parse php code. it's brittle , break simple examples in glorious ways.

point in case:

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

fatal error: uncaught exception: complex context definition in

https://3v4l.org/cemqv


Comments

Popular posts from this blog

php - SPIP: From Tag directly to an article -

jquery - isAjaxRequest always return false -

ruby on rails - In a controller spec, how to find a specific tag in the generated view? -