272

Is it possible?

function test()
{
  echo "function name is test";
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
omg
  • 136,412
  • 142
  • 288
  • 348
  • 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? – DisgruntledGoat Jun 17 '09 at 11:33
  • 39
    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. – Brian Ramsay Jun 17 '09 at 11:48
  • 1
    Needed this for logging! thanks for asking :) – Rick Kukiela Jul 31 '12 at 17:09
  • This is also useful for calling a recursive function. – uınbɐɥs Oct 19 '12 at 03: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. – Mafia Feb 26 '13 at 20:37
  • this is also useful for URL – Limon Jan 23 '14 at 18:47
  • 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. – John Boe Mar 31 '16 at 11:56
  • Useful if you have a lot of similar (but not identical) functions but need logging customized to each, e.g.: `error_log('myquery.php?foo:' . pg_last_error()` you can do something like: `error_log(__FILE__ . '?' . __FUNCTION__ . ':' . pg_last_error())` – Jacob Lee Sep 05 '19 at 22:28

4 Answers4

442

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

Example:

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

Result: MethodA.

totymedli
  • 29,531
  • 22
  • 131
  • 165
Silfverstrom
  • 28,292
  • 6
  • 45
  • 57
104

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... =)

Aurelijus Rozenas
  • 2,176
  • 2
  • 28
  • 40
PatrikAkerstrand
  • 45,315
  • 11
  • 79
  • 94
  • 45
    __METHOD__ includes the class name, __FUNCTION__ is just that. The latter is equally available in the method of a class. – Alister Bulman Jun 17 '09 at 11:16
  • 3
    That's true. But it's often useful to get MyClass::methodName instead of methodName. – PatrikAkerstrand Jun 17 '09 at 20:05
  • 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 Aug 20 '21 at 01:57
  • 1
    @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. – PatrikAkerstrand Aug 25 '21 at 15:01
19

If you are using PHP 5 you can try this:

function a() {
    $trace = debug_backtrace();
    echo $trace[0]["function"];
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Kevin Newman
  • 2,437
  • 1
  • 15
  • 12
  • 17
    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. – Luke Cousins Nov 15 '13 at 09:13
  • 2
    It's a bad practice to use debug_backtrace() for this purpose – Marcio Mazzucato Apr 15 '14 at 18:11
  • 1
    THIS IS THE **ONLY CORRECT WAY** i have found !!!!!!!!!!!!!!!!!! !!!!!!!!!!!!!!!! !!!!!!!!!!!!!! because `__FUNCTION__` returns parent function name (when the current function is included in parent function) – T.Todua Apr 25 '15 at 08:56
  • 5
    @tazo todua. can you give an example? – alds Feb 13 '16 at 18:42
2
<?php

  class Test {
     function MethodA(){
         echo __FUNCTION__ ;
     }
 }
 $test = new Test;
 echo $test->MethodA();
?>

Result: "MethodA";

Yasin Patel
  • 5,624
  • 8
  • 31
  • 53
Snehal K
  • 29
  • 1
  • While this code snippet may be the solution, [including an explanation](//meta.stackexchange.com/questions/114762/explaining-entirely-??code-based-answers) 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. – SilverNak Apr 02 '18 at 09:27