Home  >  Article  >  Backend Development  >  How does a PHP function return the function name?

How does a PHP function return the function name?

WBOY
WBOYOriginal
2024-04-11 08:39:02512browse

Get the name of the executing function through the FUNCTION magic constant in PHP: Syntax: $function_name = __FUNCTION__; Usage: Use FUNCTION inside the function to get the name of the calling function; Note: It can only be used inside the function, and only the function name is returned. , does not contain a namespace or class name, and returns an empty string when used as an anonymous function.

PHP 函数如何返回函数名?

#How does a PHP function return the function name?

A powerful feature in PHP is the ability to get the name of the function being executed via the __FUNCTION__ magic constant. This constant can be used inside a function to get the name of the calling function.

Syntax:

$function_name = __FUNCTION__;

Practical case:

The following is an example of getting the function name inside the function:

function get_function_name() {
  echo "当前函数名:" . __FUNCTION__;
}

get_function_name();

This code will output:

当前函数名:get_function_name

Other notes:

  • __FUNCTION__ Constants can only be used inside functions .
  • This constant returns only the name of the function, not the namespace or class name.
  • If the function has no name (such as an anonymous function), __FUNCTION__ will return an empty string.

The above is the detailed content of How does a PHP function return the function name?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn