Home Backend Development PHP Problem php calls the current method

php calls the current method

May 06, 2023 pm 12:02 PM

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();
Copy after login

Output result:

myMethod
MyClass::myMethod
MyClass
Copy after login

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
}
Copy after login

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();
Copy after login

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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24