The parameters that I am talking about are __FILE__
and __LINE__
- those of the caller of the function, so that the function can use them in error reporting.
Let's say that I have two files and line 100 of file_1.php calls my_func()
in file_2.php
I'd like to make that call my_func(__FILE__, __LINE__)
so that if my_func
encounters an error it can report it against file_1.php at line 100.
I do that since there are hundreds of calls to my_func
and reporting the error to be in my_func() itself might not be informative (unless I dump the stack). And I don't want to have to manually type those two parameters a few hundred times.
In C I would do something like #define MY_FUNC my_func(__FILE, __LINE)
- can I do something similar in PHP?
Unfortunately, PHP does not have any kind of macros. You could implement these yourself by running a command line tool like sed
over some input to insert macros, but that is error prone and a bit sloppy.
To do what you want to do, you might consider the debug_backtrace function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With