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

2019-07-24 11:08:50 +08:00
 Nicolay
如题,但是得到的是固定的基类的 class function args 属性的字符串,请问有什么办法可以实现吗,
2869 次点击
所在节点    PHP
2 条回复
mcfog
2019-07-24 11:23:02 +08:00
static::class 是 late binding 的,或者 get_class($this),__FUNCTION__永远是字面上的那个,如果你要的是类似 debug log 的功能,那要的可能是调用你这个基类方法的那个方法的名字,那就只能从 backtrace 里拿

另外,我觉得你可能更需要的是 xdebug
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);
}
?>
```

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/585699

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX