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

前端 未结 3 842
再見小時候
再見小時候 2020-12-04 19:17

Imagine I have the following situation:

File1.php


Function.php



        
3条回答
  •  我在风中等你
    2020-12-04 19:39

    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.

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

    A final option would be just pass __FILE__ and __LINE__ in as parameters when you call your log() function.

提交回复
热议问题