Get the __FILE__ constant for a function's caller in PHP

后端 未结 2 1954
你的背包
你的背包 2021-02-13 13:07

I know the __FILE__ magic constant in PHP will turn into the full path and filename of the currently executing file. But is there a way I can get the same informati

2条回答
  •  盖世英雄少女心
    2021-02-13 13:36

    you should take a look at the stack trace for doing such things. PHP have a function called debug_backtrace

    include "bar.php";
    call_it();
    
    //bar.php
    function call_it() {
       $bt =  debug_backtrace();
    
       echo "Calling file: ". $bt[0]['file'] . ' line  '. $bt[0]['line'];
    }
    

    hope it helps

    on the same principle you could find debug_print_backtrace useful, it do the same things but php handle the formating and printing of all the information by itself.

提交回复
热议问题