PHP, is it possible to call a function from one function without specifying the function name?

Is it possible to call a function inside the same function without specifying the function name - for example using some magic keyword?

+3


source to share


2 answers


Yes. The constant __FUNCTION__

gives the string representation of the current function. ( src )

function testMe() {
  print __FUNCTION__;
}

testMe(); // outputs "testMe"

      



You can of course use this to call yourself:

$func = __FUNCTION__;
$func();

      

+5


source


function someFunction($i)
{
     $method = __FUNCTION__;
     if ( $i > 0 )
     {
         return $method($i-1);
     }
     return $i;
}

      



a simple example of recursion, not knowing the function name, will call itself for $i

-times if $i

positive.

0


source







All Articles