omg
omg

Reputation: 140112

How do I get the function name inside a function in PHP?

Is it possible?

function test()
{
  echo "function name is test";
}

Upvotes: 278

Views: 153644

Answers (4)

Snehal K
Snehal K

Reputation: 29

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

Result: "MethodA";

Upvotes: 2

Silfverstrom
Silfverstrom

Reputation: 29348

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

Example:

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

Result: MethodA.

Upvotes: 453

Kevin Newman
Kevin Newman

Reputation: 2447

If you are using PHP 5 you can try this:

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

Upvotes: 20

PatrikAkerstrand
PatrikAkerstrand

Reputation: 45731

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

Upvotes: 107

Related Questions