我真的很讨厌写这个问题,因为我是一种“研究人员”,而且,我总是能找到我正在寻找的东西......但这一个让我很烦恼,我在任何地方都找不到答案...所以,事情是这样的:
正如标题所示,我需要获取一个方法名称,其中尾随类和命名空间路径作为字符串。我的意思是这样的:"System\Language\Text::loadLanguageCache"
。
众所周知,您可以通过键入来获取类名(具有完整的命名空间路径),即:Text::class
它返回"System\Language\Text"
,但是有没有办法得到这个方法呢?就像是:Text::loadLanguageCache::function
获取字符串:"System\Language\Text::loadLanguageCache"
?
Edit:
我想我应该进一步解释一下......我知道魔法常数__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"
}
我希望这能澄清我的问题:)
您必须使用魔法常数,您可以在以下位置阅读更多信息魔法常数 http://php.net/manual/en/language.constants.predefined.php在 php.net 中
__METHOD__
在课堂之外你必须使用反射 https://www.php.net/manual/en/book.reflection.php正如所解释的反射类 https://www.php.net/manual/en/reflectionclass.getmethods.php文档:
<?php
$class = new ReflectionClass('ReflectionClass');
$method = $class->getMethod('getMethod');
var_dump($method);
?>;
Returns:
object(ReflectionMethod)#2 (2) {
["name"]=>
string(9) "getMethod"
["class"]=>
string(15) "ReflectionClass"
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)