Is it possible to call function within the same function without specifying the function name - e.g by using some sort of magic keyword?
Yes. The constant __FUNCTION__
gives you a string representation of the current function. (src)
function testMe() {
print __FUNCTION__;
}
testMe(); // outputs "testMe"
You can then of course use this to call itself:
$func = __FUNCTION__;
$func();
function someFunction($i)
{
$method = __FUNCTION__;
if ( $i > 0 )
{
return $method($i-1);
}
return $i;
}
simple example of recursion without knowing the functions name, will call itself for $i
-times if $i
is positiv.