Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

function

php

People also ask

Can you call a function inside a function PHP?

PHP has a huge collection of internal or built-in functions that you can call directly within your PHP scripts to perform a specific task, like gettype() , print_r() , var_dump , etc.

What is the function name in PHP?

Function names follow the same rules as other labels in PHP. A valid function name starts with a letter or underscore, followed by any number of letters, numbers, or underscores. As a regular expression, it would be expressed thus: ^[a-zA-Z_\x80-\xff][a-zA-Z0-9_\x80-\xff]*$ . See also the Userland Naming Guide.

What is __ function __ in PHP?

__FUNCTION__ returns only the name of the function. while as __METHOD__ returns the name of the class alongwith the name of the function.

Which option is valid function name?

5. Which of the following are valid function names? Explanation: A valid function name can start with a letter or underscore, followed by any number of letters, numbers, or underscores. According to the specified regular expression ([a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*), a function name like this one is valid.


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

Example:

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

Result: MethodA.


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


If you are using PHP 5 you can try this:

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

<?php

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

Result: "MethodA";