Nicolay
V2EX  ›  PHP

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

  •  
  •   Nicolay · Jul 24, 2019 · 3611 views
    This topic created in 2505 days ago, the information mentioned may be changed or developed.
    如题,但是得到的是固定的基类的 class function args 属性的字符串,请问有什么办法可以实现吗,
    2 replies    2019-07-24 13:04:52 +08:00
    mcfog
        1
    mcfog  
       Jul 24, 2019   ❤️ 1
    static::class 是 late binding 的,或者 get_class($this),__FUNCTION__永远是字面上的那个,如果你要的是类似 debug log 的功能,那要的可能是调用你这个基类方法的那个方法的名字,那就只能从 backtrace 里拿

    另外,我觉得你可能更需要的是 xdebug
    ben1024
        2
    ben1024  
       Jul 24, 2019
    解析 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);
    }
    ?>
    ```
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   4904 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 09:54 · PVG 17:54 · LAX 02:54 · JFK 05:54
    ♥ Do have faith in what you're doing.