获取在 PHP 中执行当前函数的代码行和文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1252529/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:44:30  来源:igfitidea点击:

Get code line and file that's executing the current function in PHP?

php

提问by Tom

Imagine I have the following situation:

想象一下我有以下情况:

File1.php

文件1.php

<?php
 include("Function.php");
 log("test");
?>

Function.php

函数.php

<?php
 function log($msg)
 {
  echo "";
 }
?>

I want to change the log function so that it would produce the following:

我想更改日志功能,以便它会产生以下内容:

test (file: File1.php, line number: 3)

测试(文件:File1.php,行号:3)

So, any way to get the file name and the line number of the code that executed the current function in PHP?

那么,有什么方法可以在PHP中获取执行当前函数的代码的文件名和行号?

EDIT for backlog usage comments:When I use backlog in my object oriented way of programming I have the following situation.

编辑积压使用评论:当我在面向对象的编程方式中使用积压时,我有以下情况。

Index.php

索引.php

<?php
 include("Logger.static.php");
 include("TestClass.class.php");
 new TestClass();
?>

TestClass.class.php

测试类.class.php

<?php
 class TestClass
 {
   function __construct()
   {
     Logger::log("this is a test log message");
   }
 }
?>

Logger.static.php

记录器.static.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 "Index.php" and as line number 4, this is where the class is initiated. However, it is supposed to return the file TestClass.class.php and line number 6. Any idea how to fix this?

此示例将作为文件“Index.php”和第 4 行返回,这是启动类的位置。但是,它应该返回文件 TestClass.class.php 和第 6 行。知道如何解决这个问题吗?

回答by Lior Cohen

You can use debug_backtrace().

您可以使用 debug_backtrace()。

http://us3.php.net/manual/en/function.debug-backtrace.php

http://us3.php.net/manual/en/function.debug-backtrace.php

So, in your log function, you would be able to retrieve the filename and line number from which the log function was called.

因此,在您的 log 函数中,您将能够检索调用 log 函数的文件名和行号。

I'm using this approach in my logging classes and it has significantly reduced the amount of code required to get meaningful log data. Another benefit would be readability. Magic constants tend to get quite ugly when mixed with strings.

我在我的日志类中使用了这种方法,它大大减少了获取有意义的日志数据所需的代码量。另一个好处是可读性。当与字符串混合时,魔术常量往往会变得非常难看。

Here's a quick example:

这是一个快速示例:

function log($msg)
{
  $bt = debug_backtrace();
  $caller = array_shift($bt);

  // echo $caller['file'];
  // echo $caller['line'];

  // do your logging stuff here.    
}

回答by zombat

debug_backtrace()can be used to trace back through the call stack. It can be slow though, so be careful with it if you're doing a lot of logging.

debug_backtrace()可用于回溯调用堆栈。不过它可能会很慢,所以如果你要进行大量日志记录,请小心。

If you're using PHP 5.3, you could take advantage of late static bindingand have a base class method of log(), and your child classes could call it but still maintain static references to __FILE__and __LINE__.

如果您使用的是 PHP 5.3,您可以利用后期静态绑定并拥有 的基类方法log(),您的子类可以调用它但仍保持对__FILE__和 的静态引用__LINE__

A final option would be just pass __FILE__and __LINE__in as parameters when you call your log()function.

最后一个选项是在调用函数时作为参数传入__FILE__和传入。__LINE__log()

回答by ArtisticPhoenix

This is an old question but seeing as how my solution is not here, I'll provide it for posterity

这是一个老问题,但看到我的解决方案不在这里,我会为后代提供它

     try{
        throw new Exception();
    }catch ( Exception $e ){
        $trace = $e->getTrace();
    }

    $length = 0;

    foreach ($trace as $t){
        if( $t['file'] != __FILE__ ){
            break;
        }
        ++$length;
    }
    return array_slice( $trace, ($length - count( $trace ) ));

You can throw/catch to get a clean stack trace, then you need to look for the first line that contains this file ( typically that is where it is called from ) you can also use the index of the trace if you know it, or the function.

您可以 throw/catch 以获得干净的堆栈跟踪,然后您需要查找包含此文件的第一行(通常是从那里调用它的地方)您也可以使用跟踪的索引(如果您知道的话),或者功能。

The exception stack trace is pretty much the same as doing debug_backtrace(true).

异常堆栈跟踪与执行debug_backtrace(true).