280

Is it possible?

function test()
{
  echo "function name is test";
}
7
  • 2
    Just out of curiosity, when is there a need for this? Is it possible to create functions that you don't know the name of? Commented Jun 17, 2009 at 11:33
  • 42
    One possible use would be logging your execution. If you're writing "I had an error in " . FUNCTION to a logfile or something. This way, if the function name is changed you don't have to worry about the person remembering to change the log message. Commented Jun 17, 2009 at 11:48
  • 1
    This is also useful for calling a recursive function. Commented Oct 19, 2012 at 3:57
  • 2
    Also useful if you want to use the function name inside the function (for another use). Like to construct links based on the function, etc. (eg: function name1() then use name1 again inside), would save lots of time if you have the same template for lots of functions. Commented Feb 26, 2013 at 20:37
  • 1
    @DisgruntledGoat: it is useful if you create functions manually and do not want to edit error message for every function. Adding it into error message means you have less work. Commented Mar 31, 2016 at 11:56

5 Answers 5

456

The accurate way is to use the __FUNCTION__ predefined magic constant.

Example:

class Test {
    function MethodA(){
        echo __FUNCTION__;
    }
}

Result: MethodA.

Sign up to request clarification or add additional context in comments.

1 Comment

FUNCTION works for non-class functions as well. Tried on PHP 7.0.8.
107

You can use the magic constants __METHOD__ (includes the class name) or __FUNCTION__ (just function name) depending on if it's a method or a function... =)

4 Comments

METHOD includes the class name, FUNCTION is just that. The latter is equally available in the method of a class.
That's true. But it's often useful to get MyClass::methodName instead of methodName.
METHOD does not necessarily return the class name; if the place of calling this is inside a trait, you'll get the trait name. If you want to get the classname of the instance used, use get_class($this).
@Jonny: Traits were introduced in PHP 5.4, in 2012. So they weren't a thing when this answer was written in 2009. Please feel free to suggest an edit to update the answer for how PHP works nowadays.
20

If you are using PHP 5 you can try this:

function a() {
    $trace = debug_backtrace();
    echo $trace[0]["function"];
}

4 Comments

This is incredible resource intensive. Using FUNCTION or METHOD is much more efficient. debug_backtrace() is great if you need more than just the function name though.
It's a bad practice to use debug_backtrace() for this purpose
THIS IS THE ONLY CORRECT WAY i have found !!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!! !!!!!!!!!!!!!! because __FUNCTION__ returns parent function name (when the current function is included in parent function)
@tazo todua. can you give an example?
2
<?php
   
  class Test {
     function MethodA(){
         return __FUNCTION__ ;
     }
 }
 $test = new Test;
 echo $test->MethodA();
?>

Result: "MethodA";

2 Comments

While this code snippet may be the solution, including an explanation really helps to improve the quality of your post. Remember that you are answering the question for readers in the future, and those people might not know the reasons for your code suggestion.
You have to return the value instead echo, from the function to echo the result.
0
<?php
function fun(){
    var_dump([
        "__FUNCTION__" => __FUNCTION__,
        "__METHOD__" => __METHOD__,
    ]);
}
class C{
    function meth(){
        var_dump([
            "__FUNCTION__" => __FUNCTION__,
            "__METHOD__" => __METHOD__,
        ]);
    }
    static function static_meth(){
        var_dump([
            "__FUNCTION__" => __FUNCTION__,
            "__METHOD__" => __METHOD__,
        ]);
    }
}
fun();
$o = new C();
$o->meth();
$o->static_meth();
?>

outputs

array(2) {
  ["__FUNCTION__"]=>
  string(3) "fun"
  ["__METHOD__"]=>
  string(3) "fun"
}
array(2) {
  ["__FUNCTION__"]=>
  string(4) "meth"
  ["__METHOD__"]=>
  string(7) "C::meth"
}
array(2) {
  ["__FUNCTION__"]=>
  string(11) "static_meth"
  ["__METHOD__"]=>
  string(14) "C::static_meth"
}

Fwiw if you want to call yourself without knowing/caring about your function name, you'd do

(__METHOD__)();

or perhaps

(__METHOD__)(...func_get_args());

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.