macros in PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

macros in PHP

Post 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
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post by protokol »

No, there are no macros, but you can probably use smarty to do something similar with templates.
jasongr
Forum Contributor
Posts: 206
Joined: Tue Jul 27, 2004 6:19 am

Post 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
User avatar
protokol
Forum Contributor
Posts: 353
Joined: Fri Jun 21, 2002 7:00 pm
Location: Cleveland, OH
Contact:

Post 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.
Post Reply