你能从 PHP 的方法中获取方法名称吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1424846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:31:44  来源:igfitidea点击:

Can you get a method name from within a method in PHP?

phpmethodsoop

提问by alex

Is it possible to do something like this?

有可能做这样的事情吗?

public function something() {
    $thisMethodName = method_get_name(); 
}

Where method_get_name()returns the method's name?

哪里method_get_name()返回方法的名称?

回答by Kevin Vaughan

Sure, you want the magic constants.

当然,你想要魔法常数。

function myFunction() { print __FUNCTION__." in ".__FILE__." at ".__LINE__."\n"; }

Find out more from the php manual

php 手册中了解更多信息

回答by thesmart

While you can use the magic constant__METHOD__I would highly recommend checking out PHP's reflection. This is supported in PHP5.

虽然您可以使用魔法常量,但__METHOD__我强烈建议您查看 PHP 的反射。这在 PHP5 中受支持。

$modelReflector = new ReflectionClass(__CLASS__);
$method = $modelReflector->getMethod(__METHOD__);

You can then do kick-ass stuff like inspect the signature, etc.

然后你可以做一些像检查签名这样的事情。

回答by Jan Molak

As smartj suggested you can try the magic constant __METHOD__, but remember that this will return a string containing also your class name, i.e. 'MyClass::something'. Using __FUNCTION__instead will return 'something'.

正如 smartj 建议的那样,您可以尝试使用魔法常量__METHOD__,但请记住,这将返回一个还包含您的类名的字符串,即“MyClass::something”。使用__FUNCTION__反而会返回“东西”。

回答by Anonymous Visitor

Using __FUNCTION__is the way to go instead of:

使用__FUNCTION__是要走的路,而不是:

 public function something() {
     $thisMethodName = "something";
 }

which is flawed in several ways adding a variable and memory to store the method name as a string and duplicating what already exists, thus unnecessarily adding resources used, (if you do this for a large library with many methods, it matters greatly).

这在几个方面存在缺陷,添加变量和内存以将方法名称存储为字符串并复制已经存在的内容,从而不必要地添加使用的资源,(如果您为具有许多方法的大型库执行此操作,则非常重要)。

Magic constants in PHP are guaranteed not to change, while this approach would require applicable editing if the method name were changed, thus introducing the potential for an inconsistency (note, I did say potentially, meaning simply it is an otherwise unnecessary edit if the magic constant were used instead).

PHP 中的魔法常量保证不会改变,而如果方法名称改变,这种方法将需要适用的编辑,从而引入不一致的可能性(注意,我确实说过潜在的,这意味着如果魔法改为使用常数)。

The time and effort to name a variable, re-type the method name as a string assigned to that unnecessary variable and of course properly referencing the variable name, which is the motivation for PHP supplying magic constants to begin with (and refuting any claim __FUNCTION__is unnecessary).

命名变量的时间和精力,重新键入方法名称作为分配给该不必要变量的字符串,当然正确引用变量名称,这是 PHP 提供魔术常量的动机开始(并驳斥任何主张__FUNCTION__是不必要)。

回答by timdev

Hackish, but you could also probably dig it out of the debug_backtrace() return value.

Hackish,但你也可以从 debug_backtrace() 返回值中挖掘它。

回答by Илья Зеленько

With __FUNCTION__i can use this:

随着__FUNCTION__我可以用这个:

protected static function getUserResponseByAccessTokenRequestOptions(string $myParam): array
{
    return array_merge(parent::{__FUNCTION__}($myParam), [
        'myValue' => '123'
    ]);
}

instead of this:

而不是这个:

protected static function getUserResponseByAccessTokenRequestOptions(string $myParam): array
{
    return array_merge(parent::getUserResponseByAccessTokenRequestOptions($myParam), [
        'myValue' => '123'
    ]);
}

And does not care about replacing the name of the method inside method if I want to change it.

如果我想改变它,也不关心在方法内部替换方法的名称。

回答by sdotbertoli

Perhaps missing late bindings in this way.

也许以这种方式缺少后期绑定。

when class B extend class A:

当 B 类扩展 A 类时:

__METHOD__always return the "className::" part referred to A (A::methodName)

__METHOD__始终返回引用 A ( A::methodName)的“className::”部分

Solution is:

解决办法是:

static::class."::".__FUNCTION__

static::class."::".__FUNCTION__

In this way you obtain the class reference to current working class (B::methodName)

通过这种方式,您可以获得当前工作类的类引用 ( B::methodName)

回答by Andrew Hare

Why can't you do this?

为什么你不能这样做?

public function something() {
    $thisMethodName = "something";
}