Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I make a PHP "macro" (like #define) to supply parameters for function calls?

Tags:

php

macros

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?

like image 326
Mawg says reinstate Monica Avatar asked Oct 12 '10 02:10

Mawg says reinstate Monica


1 Answers

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.

like image 131
ryeguy Avatar answered Sep 20 '22 14:09

ryeguy