PHP如何获得一个以字符串形式包含类和命名空间路径的方法名


PHP How to get a method name with a class and namespace path as a string?

我真的不想写这个问题,因为我是一个"研究者",而且,我总是能找到我要找的东西。。。但这件事让我很困扰,我在任何地方都找不到答案。。。所以,它开始了:

正如标题所说,我需要获得一个方法名称,后面的类和命名空间路径为字符串。我的意思是:"System'Language'Text::loadLanguageCache"。正如我们所知,您可以通过键入(即:Text::class)来获得类名(具有完整的命名空间路径),并且它返回"System'Language'Text",但有没有方法可以为方法获得该类名?类似于:Text::loadLanguageCache::function来获取字符串:"System'Language'Text::loadLanguageCache"

编辑:

我想我应该进一步解释一下。。。我知道魔术常数__METHOD__,但问题是它在被调用的方法内部工作,我需要这个"方法外部"。举个例子:

//in System'Helpers
function someFunction()
{ return __METHOD__; }

如果我调用我将得到的函数(假设该方法在System'Helpers类中)-"System'Helpers::someFunction",这一切都可以。但我想要的是:

//in System'Helpers
function someFunction()
{ //some code... whatever }
// somewhere not in System'Helpers
function otherFunction()
{
    $thatFunctionName = Helpers::someFunction::method //That imaginary thing I want
    $thatClassName = Helpers::class; //this returns "System'Helpers"
}

我希望这能澄清我的问题:)

您必须使用魔术常数,您可以在php.net 中阅读更多关于魔术常数的信息

__METHOD__

在类之外,您必须使用ReflectionClass文档中解释的反射:

<?php
$class = new ReflectionClass('ReflectionClass');
$method = $class->getMethod('getMethod');
var_dump($method);
?>;

退货:

object(ReflectionMethod)#2 (2) {
  ["name"]=>
  string(9) "getMethod"
  ["class"]=>
  string(15) "ReflectionClass"
}