Page 1 of 1

macros in PHP

Posted: Thu Dec 02, 2004 10:45 am
by jasongr
Hello

I have the following code which repeats numerous times in my applicaion:

Code: Select all

$memoryManager->checkMemoryQuota($param1, $param2);
I would like to be able to define a macro called MEM which will translate to the above code.
So I will be able to write code like so:

Code: Select all

MEM($param1, $params);
which is much shorter
Is it possible in PHP?
It is important to note that function checkMemoryQuota actually takes 3 parameters (the 3rd parameter is optional). I would still like the macro to work without having to define 2 macros

thanks in advance

Posted: Thu Dec 02, 2004 11:30 am
by protokol
No, there are no macros, but you can probably use smarty to do something similar with templates.

Posted: Thu Dec 02, 2004 11:32 am
by jasongr
I see

I can also define a global function called TEXT that will do a simple redirect.
But this adds overhead of yet another function call, even though the code does become shorter

Posted: Thu Dec 02, 2004 11:35 am
by protokol
I personally prefer to leave it as-is even if the function calls are pretty long strings. It disambiguates the code by not making the user find where the 'macro' came from. Also, as you mentioned, you don't have the overhead of creating some sort of wrapper for this functionality.