6

I'm trying to solve a problem :

function f($callable_function){
   print_r($callable_function);// doesn't work at all, of course
}

f(function(){echo "hello World"});

I get the Closure Object element with my print_r function. Is there a way to get :

//OUTPUT RESULT
echo "hello World";

EDIT

The purpose of this is to get the function declaration inside a String (for database later). The f function can be used by any developer so the idea is to make this as simple as possible, that means I don't want the developer to declare his function inside of a String.

d3cima
  • 729
  • 1
  • 10
  • 31
  • PHP is not JavaScript, you cannot print out source code like that... – ItalyPaleAle Aug 26 '14 at 13:22
  • 1
    You can only get `hello world` not along with `echo ` – Rahil Wazir Aug 26 '14 at 13:31
  • If you want to actually execute the function and get the output from it, then do so, I don't know what you thought you would get by using `print_r` on it. – Mike Brant Aug 26 '14 at 13:35
  • I know php doesn't work like javascript but is there a way to get the function declaration into a string, I don't want to execute the function itself. – d3cima Aug 26 '14 at 13:36
  • Why a function made to dump an array should do something so ? – Brewal Aug 26 '14 at 13:36
  • I'm just searching a way to get the declaration, the idea is to store the function declaration. – d3cima Aug 26 '14 at 13:37
  • 2
    Is there a goal behind this attemp ? – Brewal Aug 26 '14 at 13:42
  • 3
    You will get better answers if you describe *what* you are trying to *achieve*, instead of describing *how* you fail with a solution you think is correct. Classic XY problem. – N.B. Aug 26 '14 at 13:47

2 Answers2

4

Never closed this question but I have the answer since 2014 :

 $reflFunc = new ReflectionFunction($callable);
 $filename = $reflFunc->getFileName()); 
 $startline = $reflFunc->getStartLine(); 
 $endline = $reflFunc->getEndLine();

And using preg_match, I can extract what I need. (as suggested by the first answer)

d3cima
  • 729
  • 1
  • 10
  • 31
0

You can get the PHP file as a string and then find the function (the following exemple is a very quick one, it should be improved):

function f($callable_function) {
    $f = file_get_contents(__FILE__);
    preg_match("#function $callable_function\(.*\)[^\{]*{(.*)}#Us", $f, $matches);
    var_dump($matches);
}

function a() {
    echo "hello World";
}

f('a');

But there is some limitations:

  • It can be complicated if you have many PHP files for instance
  • My exemple not supports character { into the body of the function, but this can be improved by somebody good at regex
  • It doesn't work with anonymous callbacks (and maybe with object's method)
Fractaliste
  • 5,777
  • 11
  • 42
  • 86
  • I like this solution, this may be a replacement to my solution, but I would be happy to use anonymous functions. But thank you for you help :-) – d3cima Aug 26 '14 at 14:33
  • This answer helped a lot : Insted of preg_match function name, I used : $reflFunc = new ReflectionFunction($callable); $file = file_get_contents($reflFunc->getFileName()); $reflFunc->getStartLine(); $reflFunc->getEndLine(); – d3cima Aug 26 '14 at 22:49
  • if the function is declared dynamically (callable sometime is), then this could be long search for right file across the project – Jimmmy Feb 20 '19 at 17:03