V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Nicolay
V2EX  ›  PHP

想把 __CLASS__ . '_' . __FUNCTION__ . '_' .json_encode(func_get_args()) 写在基类可以给子类统一调用

  •  
  •   Nicolay · 2019-07-24 11:08:50 +08:00 · 2859 次点击
    这是一个创建于 1731 天前的主题,其中的信息可能已经有所发展或是发生改变。
    如题,但是得到的是固定的基类的 class function args 属性的字符串,请问有什么办法可以实现吗,
    2 条回复    2019-07-24 13:04:52 +08:00
    mcfog
        1
    mcfog  
       2019-07-24 11:23:02 +08:00   ❤️ 1
    static::class 是 late binding 的,或者 get_class($this),__FUNCTION__永远是字面上的那个,如果你要的是类似 debug log 的功能,那要的可能是调用你这个基类方法的那个方法的名字,那就只能从 backtrace 里拿

    另外,我觉得你可能更需要的是 xdebug
    ben1024
        2
    ben1024  
       2019-07-24 13:04:52 +08:00
    解析 debug_backtrace() 里面的参数,
    https://www.php.net/manual/zh/function.debug-backtrace.php

    ```php
    <?php
    function generateCallTrace()
    {
    $e = new Exception();
    $trace = explode("\n", $e->getTraceAsString());
    // reverse array to make steps line up chronologically
    $trace = array_reverse($trace);
    array_shift($trace); // remove {main}
    array_pop($trace); // remove call to this method
    $length = count($trace);
    $result = array();

    for ($i = 0; $i < $length; $i++)
    {
    $result[] = ($i + 1) . ')' . substr($trace[$i], strpos($trace[$i], ' ')); // replace '#someNum' with '$i)', set the right ordering
    }

    return "\t" . implode("\n\t", $result);
    }
    ?>
    ```
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1169 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 18:18 · PVG 02:18 · LAX 11:18 · JFK 14:18
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.