感谢 pos dupe 链接,我设法进行了一些挖掘以真正得到我想要的东西。它似乎debug_backtrace()
很好.. 追溯每个函数调用。例如
文件A.php
class Bar
{
public function foo()
{
echo '<pre>'. print_r(debug_backtrace(), 1) .'</pre>';
return 'hi';
}
}
文件B.php
require_once 'fileA.php';
$bar = new \Bar();
echo $bar->foo();
这输出:
Array
(
[0] => Array
(
[file] => /var/www/testing/test/fileB.php
[line] => 5
[function] => foo
[class] => Bar
[object] => Bar Object ()
[type] => ->
[args] => Array ()
)
)
hi
这在很大程度上是完美的。但是,这并不能保证结果,因为数组每个堆栈都会增加。
例如 FileC.php 调用 FileB.php 中的函数,后者又调用 FileA.php 中的函数
但是,我在使用函数时注意到,最理想的函数是数组中的结束元素。考虑到这一点,我设置了一些函数来模拟魔法常数的功能,而不使用任何魔法。
设置使用功能:
$trace = debug_backtrace();
$call = end($trace);
目录 ( __DIR__
):
# $trace = $call['file']
protected function getDir($trace)
{
$arr = explode('/', $trace);
$file = end($arr);
$directory = [];
$i = 0;
foreach ($arr as $data)
{
if ($data !== $file) {
$directory[] = isset($output) ? $output[$i - 1] . '/' . $data : $data;
$i++;
}
}
return 'DIRECTORY: '. implode('/', $directory);
}
文件 ( __FILE__
)::
# $trace = $call['file']
protected function getFile($trace)
{
$arr = explode('/', $trace);
$file = end($arr);
return 'FILE: '. $file;
}
函数/方法 ( __FUNCTION__
|| __METHOD__
)::
# $trace = $call
protected function getFunction($trace)
{
$output = 'FUNCTION: '. $trace['function'] ."\n";
foreach ($trace['args'] as $key => $arguments)
{
foreach ($arguments as $k => $arg)
{
if (!is_array($arg)) {
$output .= 'ARGS ('. $k .'): '. $arg ."\n";
}
}
}
return $output;
}
命名空间 ( __NAMESPACE__
):
# $trace = $call['class']
protected function getNamespace($trace)
{
$arr = explode('\\', $trace);
$class = end($arr);
$namespace = [];
$i = 0;
foreach ($arr as $data)
{
if ($data !== $class) {
$namespace[] = isset($output) ? $output[$i - 1] . '/' . $data : $data;
$i++;
}
}
return 'NAMESPACE: '. implode('\\', $namespace);
}
类(__CLASS__
):
# $trace = $call['class']
protected function logClass($trace)
{
if (strpos($trace, '\\') !== false) {
$arr = explode('\\', $trace);
$class = end($arr);
} else {
$class = $trace;
}
$return = 'CLASS: '. $class;
}
缺少魔法常数:
线路是可访问的(正如您从 中看到的那样print_r($call, 1)
),但我不需要/不感兴趣。Trait 或多或少与__NAMESPACE__
我的用途相同,因此,它对为其创建函数也不感兴趣。
笔记: