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.
- 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.
- 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.
- 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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool
