kuba
kuba

Reputation: 3870

How to define macro with parameter in PHP as it is possible in C++?

Target is to have something like:

#define sum( f1, f2 ) ( f1 + f2 )

where

sum(2,2)

is 4 in PHP. Is that somehow possible? Even better would be if I could call:

$s = 'first';
APPEND ' and second';

when append will be defined as function/method/something else which is appending to $s so after those 2 lines $s would be 'first and second'.

Upvotes: 1

Views: 7220

Answers (4)

John Franklin
John Franklin

Reputation: 4912

I don't believe PHP supports macros. If you think about it, macros in C aren't fancy constants, rather constants are the world's most boring C macros. PHP only understands the boring form through the define function.

That said, I disagree with the other respondents about their validity. There is a valid reason for them: when you want to use built-in functions to preprocess something. For example:

macro default_if_empty_value(value, default) (empty(value) ? default : value)

If you try to write that as a function and call it:

default_if_empty_value($this->that['something']->or[$other], 'Not here.');

then you'll get a PHP warning if any part of that variable chain is not defined, and the expanded version is harder to read:

empty($this->that['something']->or[$other]) ? 'Not here.' : $this->that['something']->or[$other];

PHP code is compiled, but it is compiled at runtime. A macro like this would be expanded by the PHP interpreter and then compiled into PHP bytecode. This bytecode is often cached by the PHP interpreter or an add-on like APC.

Upvotes: 2

houbysoft
houbysoft

Reputation: 33412

The point of macros in C is that they are expanded at compile time.

Therefore, using the macros does not have an impact on the speed of your code, which a function doing the same would have.

Therefore, an example of usage is:

#define MYPRINTF(x) printf("MYPRINTF says : %s\n",x)
MYPRINTF("blah");

The code above will be translated by the compiler directly into:

printf("MYPRINTF says : %s\n","blah");

The whole point of it is that it is faster than defining a function, such as this one:

void myprintf(char *x)
{
  printf("myprintf says : %s\n","blah");
}

Because there is no overhead (in a function, you need to push arguments to the stack, etc).

In an interpreted language, like PHP, the above doesn't hold, as everything is executed directly during run-time, therefore using a mechanism like C's #define would be absolutely useless -- therefore, to answer your question, simply use ordinary functions instead.

Upvotes: 3

Phill Pafford
Phill Pafford

Reputation: 85358

just a thought from your comments

function withVeryLongWroteByAnIdiot() {
   ....
}

function xFunc() {
   return withVeryLongWroteByAnIdiot();
}

Upvotes: 2

John Parker
John Parker

Reputation: 54445

PHP doesn't support macros in this sense, the (really quite valid) argument I believe being that there's not much difference between this and a normal function in PHP.

After all, it's not like there's any value in having a concept like this in a run-time interpreted language.

Upvotes: 1

Related Questions