Home  >  Article  >  Backend Development  >  php calls the current method

php calls the current method

王林
王林Original
2023-05-06 12:02:07627browse

In PHP development, sometimes we need to get information about the current method, such as getting the name of the current method, parameter list, return value, etc. This article will introduce some methods to obtain information about the current method.

  1. Use built-in variables

PHP has some built-in variables to obtain information about the current method, including __FUNCTION__, __METHOD__, __CLASS__, __TRAIT__, etc. Among them, __FUNCTION__ can get the name of the current function, __METHOD__ can get the name of the current method (function in the class), __CLASS__ can get the name of the current class, __TRAIT__ You can get the name of the current trait.

For example:

class MyClass {
    public function myMethod() {
        echo __FUNCTION__ . "\n";
        echo __METHOD__ . "\n";
        echo __CLASS__ . "\n";
        echo __TRAIT__ . "\n";
    }
}

$myInstance = new MyClass();
$myInstance->myMethod();

Output result:

myMethod
MyClass::myMethod
MyClass

In the above code, __FUNCTION__ outputs the name of the current function myMethod, __METHOD__ outputs the name of the current method MyClass::myMethod, __CLASS__ outputs the name of the current class MyClass, __TRAIT__ outputs the name of the current trait. It should be noted that __METHOD__ outputs the complete method name, including the class name and method name.

  1. Using ReflectionClass and ReflectionMethod

ReflectionClass and ReflectionMethod are PHP’s built-in reflection classes that can be used to obtain class and method information. Use the getClassName() method of ReflectionMethod to get the name of the class where the current method is located. The getName() method can get the name of the current method. The getParameters() method can get the parameter list of the current method. The getReturnType() method can get the return value of the current method. type.

For example:

class MyClass {
    public function myMethod($param1, $param2) {
        // do something
    }
}

$myInstance = new MyClass();
$reflectMethod = new ReflectionMethod($myInstance, 'myMethod');
echo $reflectMethod->getClassName() . "\n"; // 输出 MyClass
echo $reflectMethod->getName() . "\n"; // 输出 myMethod
$params = $reflectMethod->getParameters();
foreach ($params as $param) {
    echo $param->getName() . "\n"; // 输出 param1 和 param2
}

In the above code, the myMethod method in the MyClass class is obtained through the ReflectionMethod class, and the class name, method name and parameter name are output respectively.

  1. Use debug_backtrace function

The debug_backtrace function can obtain the information of the current call stack and can be used to obtain the information of the current method. The information obtained through the debug_backtrace function is an array. The array contains information about all functions and methods in the call stack, so you need to traverse the array to find the information about the current method.

For example:

class MyClass {
    public function myMethod() {
        $trace = debug_backtrace();
        foreach ($trace as $traceItem) {
            if ($traceItem['function'] == 'myMethod') {
                echo $traceItem['class'] . "\n"; // 输出 MyClass
                echo $traceItem['function'] . "\n"; // 输出 myMethod
                break;
            }
        }
    }
}

$myInstance = new MyClass();
$myInstance->myMethod();

In the above code, the call stack information is obtained through the debug_backtrace function, and then the array is traversed to find the myMethod method information, and the class name and method name are output respectively.

Summary

This article introduces three methods to obtain the current method information, namely using built-in variables, using ReflectionClass and ReflectionMethod, and using the debug_backtrace function. Among them, using built-in variables is the simplest, but you cannot obtain the specific parameter list and return value; using ReflectionClass and ReflectionMethod requires instantiating the ReflectionClass or ReflectionMethod object, which is more cumbersome; although using the debug_backtrace function can obtain all call stack information, it requires traversing the array Finding information about the current method is time-consuming. In actual development, you can choose the appropriate method according to your needs to obtain the current method information.

The above is the detailed content of php calls the current method. 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