假设我有以下情况:
File1.php
<?php
include("作用php");
log("test");
?>
作用php
<?php
function log($msg)
{
echo "";
}
?>
我想更改日志(log)函数,使其生成以下内容:
测试(文件:File1.php,行号:3)
那么,有没有办法获取在PHP中执行当前函数的代码的文件名和行号?
EDIT for backlog usage comments:
指数php
<?php
include("记录器.静止的php");
include("测试课.班php");
new TestClass();
?>
测试课.班php
<?php
class TestClass
{
function __construct()
{
Logger::log("this is a test log message");
}
}
?>
记录器.静止的php
<?php
class Logger
{
static public function log($msg)
{
$bt = debug_backtrace();
$caller = array_shift($bt);
echo $caller['file'];
echo $caller['line'];
}
}
?>
This example will return as file "指数php" and as line number 4, this is where the class is initiated. However, it is supposed to return the file 测试课.班php and line number 6. Any idea how to fix this?