|
Posted by Tim Martin on 11/18/01 11:44
Kimmo Laine wrote:
> "Emil" <emjot_wytnij_to_@podczta.onet.pl> wrote in message
> news:e15ag1$3ee$1@news.onet.pl...
>> Is there any hope that new versions of PHP
>> will support macros similar to C or C++?
> What's the actual difference between a function and a macro? How would use
> of macros differ from functions?
>
> Let's pretend there is a way of defining a macro in php...
> define ("MAX($a,$b)", "(($a<$b)?$b:$a)");
>
> vs.
>
> function max( $a, $b ) {
> return $a < $b ? $b : $a;
> }
The difference is that macros are applied as text substitution before
parsing is carried out. This means that macros can carry out
substitutions on the code that would not in itself be a valid language
construct.
The classic example of misuse of this sort of technique in C is
#define BEGIN {
#define END }
then you can do silly things like:
for (i = 0; i < 10; i++)
BEGIN
printf("%d\n", i);
END
Used judiciously these sorts of techniques can open up all sorts of
possibilities (I've seen generic type containers implemented in pure C
using macros), but the general consensus is that the potential for
misuse is far too great and modern language constructs have obviated all
the genuine needs for such techniques.
To the OP: What are you trying to achieve with macros? With a
combination of pass-by-reference, soft references and eval() you can
achieve many of the things that C macros are able to achieve, with
better syntax and less potential for misuse.
Tim
*** Free account sponsored by SecureIX.com ***
*** Encrypt your Internet usage with a free VPN account from http://www.SecureIX.com ***
Navigation:
[Reply to this message]
|