draxq02664 2013-11-30 15:19
浏览 262

PHP如何使用类和命名空间路径作为字符串获取方法名称?

I really hate to write this question because I'm kind-of "research guy" and, well, I always find what I'm searching for... But this one bugs me a lot and I can't find the answer anywhere... So, here it goes:

As the title says I need to get a method name with the trailing class and namespace path as a string. I mean something like this: "System\Language\Text::loadLanguageCache". As we know, you can get a class name (with full namespace path) by typing, i.e.: Text::class and it returns "System\Language\Text", but is there a way to get this for a method? Something like: Text::loadLanguageCache::function to get string: "System\Language\Text::loadLanguageCache"?

Edit:

I think I should explain this further... I know about magic constant __METHOD__ but the problem is that it works inside the called method and I need this "outside the method". Take this as example:

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

That's all OK with this if I call the function I'll get (let's assume that method is in System\Helpers class) - "System\Helpers::someFunction". But what I want is this:

//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"
}

I hope this cleared my question a bit :)

  • 写回答

1条回答 默认 最新

  • douxi3404 2013-11-30 15:31
    关注

    You must use the magic constant, you can read more about at Magic constants in php.net

    __METHOD__
    

    Outside of the class you must use Reflection as is explained at ReflectionClass documentation:

    <?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"
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度