473,854 Members | 1,789 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Macros in php

Is there any hope that new versions of PHP
will support macros similar to C or C++?
I've searched manual and didn't find anything
except define directive, but it can be used
to define constant values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.

greetz Emil
Apr 7 '06 #1
47 32936
"Emil" <em************ **@podczta.onet .pl> wrote in message
news:e1******** **@news.onet.pl ...
Is there any hope that new versions of PHP
will support macros similar to C or C++?
I've searched manual and didn't find anything
except define directive, but it can be used
to define constant values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.

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;
}

And use them like this:
MAX($a,$b); // This is macro, so much easier!
max($a,$b); // This is plain old dull function! Bah, no no, not like
this...

I mean.... WTF?

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi. net | Gedoon-S @ IRCnet | rot13(xv***@bhg byrzcv.arg)
Apr 7 '06 #2
Kimmo Laine wrote:
"Emil" <em************ **@podczta.onet .pl> wrote in message
news:e1******** **@news.onet.pl ...
Is there any hope that new versions of PHP
will support macros similar to C or C++?
I've searched manual and didn't find anything
except define directive, but it can be used
to define constant values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.


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;
}

And use them like this:
MAX($a,$b); // This is macro, so much easier!
max($a,$b); // This is plain old dull function! Bah, no no, not like
this...

I mean.... WTF?


Macros are more efficient.

Personally, one construct I use heavily and would like to replace with a macro:

$var = isset($_POST['postvar']) ? $_POST['postvar'] . 'default value';

I use something similar for a get/post, session and cookie values. It would be
very nice to have a macro.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 7 '06 #3
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:GL******** *************** *******@comcast .com...
Kimmo Laine wrote:
"Emil" <em************ **@podczta.onet .pl> wrote in message
news:e1******** **@news.onet.pl ...
Is there any hope that new versions of PHP
will support macros similar to C or C++?
I've searched manual and didn't find anything
except define directive, but it can be used
to define constant values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.


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;
}

And use them like this:
MAX($a,$b); // This is macro, so much easier!
max($a,$b); // This is plain old dull function! Bah, no no, not like
this...

I mean.... WTF?


Macros are more efficient.

Personally, one construct I use heavily and would like to replace with a
macro:

$var = isset($_POST['postvar']) ? $_POST['postvar'] . 'default value';

I use something similar for a get/post, session and cookie values. It
would be very nice to have a macro.

And tell mme again why you couldn't write a function instead of a macro to
do that?

Just so we all remember what we're talking about here... In C a macro is a
syntax replacement that the precompiler uses to both optimize the code
(avoid the unnecessary function jump for a short task) and make it easy to
write for the coder. The complier simply translates a pseudo code (the
macro) to actual code when the code is compiled to executable. Since PHP is
not precompiled, I don't see how this could be of any use. The php source
code should be precompiled in order to get the replaced... Macros just don't
have a purpouse in run-time compiled language like they do in precompiled
languages.

--
"ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" -lpk
sp**@outolempi. net | Gedoon-S @ IRCnet | rot13(xv***@bhg byrzcv.arg)
Apr 7 '06 #4
Kimmo Laine wrote:
"Jerry Stuckle" <js*******@attg lobal.net> wrote in message
news:GL******** *************** *******@comcast .com...
Kimmo Laine wrote:
"Emil" <em************ **@podczta.onet .pl> wrote in message
news:e1***** *****@news.onet .pl...
Is there any hope that new versions of PHP
will support macros similar to C or C++?
I've searched manual and didn't find anything
except define directive, but it can be used
to define constant values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.


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;
}

And use them like this:
MAX($a,$b) ; // This is macro, so much easier!
max($a,$b) ; // This is plain old dull function! Bah, no no, not like
this...

I mean.... WTF?


Macros are more efficient.

Personally, one construct I use heavily and would like to replace with a
macro:

$var = isset($_POST['postvar']) ? $_POST['postvar'] . 'default value';

I use something similar for a get/post, session and cookie values. It
would be very nice to have a macro.


And tell mme again why you couldn't write a function instead of a macro to
do that?

Just so we all remember what we're talking about here... In C a macro is a
syntax replacement that the precompiler uses to both optimize the code
(avoid the unnecessary function jump for a short task) and make it easy to
write for the coder. The complier simply translates a pseudo code (the
macro) to actual code when the code is compiled to executable. Since PHP is
not precompiled, I don't see how this could be of any use. The php source
code should be precompiled in order to get the replaced... Macros just don't
have a purpouse in run-time compiled language like they do in precompiled
languages.


I know exactly what a macro is in C - I've been programming C for over 20 years.

And yes, the can still be more efficient in PHP. Remember - each page is NOT
reparsed every time it is requested. PHP can also cache pages, then pull the
code right from the cache.

So yes, they can be more efficient.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 7 '06 #5
Kimmo Laine napisa³(a):
$var = isset($_POST['postvar']) ? $_POST['postvar'] . 'default value';

I have the same problem.

And tell mme again why you couldn't write a function instead of a macro to
do that?


The reason is when you pass $_POST['postvar'] to a function and
$_POST['postvar'] is not set, PHP generates warning. Of course one could
turn off warnings and usually does, but in my opinion it's not a
solution. However checking everytime if variable (or array index) is set
before passing it to a function generates sometimes tones of similar
code which could be simply replaced with one elegant macro.
Maybe it's just a matter of programmer's habit, I feel lack of macros.

Greetz Emil
Apr 7 '06 #6
"Kimmo Laine" <sp**@outolempi .net> writes:
"Emil" <em************ **@podczta.onet .pl> wrote in message
news:e1******** **@news.onet.pl ...
Is there any hope that new versions of PHP
will support macros similar to C or C++?
I've searched manual and didn't find anything
except define directive, but it can be used
to define constant values only.
Of course it is not THAT neccessary functionality,
but it could be very useful.

What's the actual difference between a function and a macro? How would use
of macros differ from functions?


Just the other day, I was thinking that a preprocessor in PHP would be
handy. I've got a lot of code that goes

<?php
include 'cache.inc';
$c = new cache;
if ($c->uncached()) {

... main body of php code

}
$c->end(); // cache and/or display html
include 'stats_counter. inc';
?>

It would be nice to replace this with

<?php
START_CACHING;

... main body of php code

END_CACHING;
?>

but I can't see how this can be done neatly[*] with functions.
Ian

[*] It could be done badly with cache.php?inc=m ycode.php where
cache.php is something like

<?php
include 'cache.inc';
$c = new cache;
if ($c->uncached()) {

include $_GET['inc'];

}
$c->end(); // cache and/or display html
include 'stats_counter. inc';
?>
Apr 7 '06 #7
Jerry Stuckle wrote:
Macros are more efficient.

Personally, one construct I use heavily and would like to replace with a macro:

$var = isset($_POST['postvar']) ? $_POST['postvar'] . 'default value';

I use something similar for a get/post, session and cookie values. It would be
very nice to have a macro.


I think the operation is common enough that it deserves a function-like
operator. Something like

$val = coalesce($_POST['postvar'], $_SESSION['postvar'], 'default
value');

Apr 7 '06 #8
"Ian McConnell" <ia*@emit.demon .co.uk> wrote in message
news:87******** ****@emit.demon .co.uk...
Just the other day, I was thinking that a preprocessor in PHP would be
handy. I've got a lot of code that goes

<?php
include 'cache.inc';
$c = new cache;
if ($c->uncached()) {

... main body of php code

}
$c->end(); // cache and/or display html
include 'stats_counter. inc';
?>

It would be nice to replace this with

<?php
START_CACHING;

... main body of php code

END_CACHING;
?>


Any piece of software is essentially a series of mental concepts, written
down as computer code. So what you are saying (and I agree heartily) is:
See this series of PHP statements? They represent a concept called "Start
Caching".

So it makes sense to write START_CACHING rather than spell out the details.
I think it makes the code easier to read, both for the author and any other
soul who has to learn it.

This aspect of macros has nothing to do with computer efficiency, but a
great deal to do with human efficiency. We *like* to name things.
Languages should accomodate this.

What's nice about macros is that they can be used to name rather arbitrary
stuff, including fragments of code. Functions have a different role to
play.

-Dana
Apr 7 '06 #9
> What's the actual difference between a function and a macro? How would use
of macros differ from functions?


I was working on modular system, where every module was a file with
object definition.

I needed to make some global variables accessible in method of this
object (i.e. database connection object) in every module, but I wanted
to have a list of these variables only once in whole system (it would be
unpleasant to edit all modules if I decided to, for example, change name
of any of these variables).

Function is useless in this case, so I needed a macro.

I solved a problem this way:

// In initialisation part of code
define('MY_MACR O', 'global $database, $global_config,
$any_other_need ed_variables;') ;

// In beginning of module object's method
eval (MY_MACRO);

But maybe there is much better way to do this
Apr 7 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

21
3001
by: Chris Reedy | last post by:
For everyone - Apologies for the length of this message. If you don't want to look at the long example, you can skip to the end of the message. And for the Python gurus among you, if you can spare the time, I would appreciate any comments (including words like evil and disgusting, if you think they are applicable :-}) on the example here. Kenny -
699
34378
by: mike420 | last post by:
I think everyone who used Python will agree that its syntax is the best thing going for it. It is very readable and easy for everyone to learn. But, Python does not a have very good macro capabilities, unfortunately. I'd like to know if it may be possible to add a powerful macro system to Python, while keeping its amazing syntax, and if it could be possible to add Pythonistic syntax to Lisp or Scheme, while keeping all of the...
16
2432
by: mike420 | last post by:
Tayss wrote: > > app = wxPySimpleApp() > frame = MainWindow(None, -1, "A window") > frame.Show(True) > app.MainLoop() > Why do you need a macro for that? Why don't you just write
37
2824
by: michele.simionato | last post by:
Paul Rubin wrote: > How about macros? Some pretty horrible things have been done in C > programs with the C preprocessor. But there's a movememnt afloat to > add hygienic macros to Python. Got any thoughts about that? "Movement" seems quite an exaggeration. Maybe 2-3 people made some experiments, but nobody within the core Python developers seems to be willing to advocate the introduction of macros. > Why should you care whether the...
8
7243
by: Michael Winter | last post by:
In a recent post ("About C error" by Victor, 21 Sep 2003), comments were made about the poster's use of macros. What I would like to know is why they are considered bad? I'm not referring to their use as 'functions'; I realise the loss of type-safety and the possible evaluation errors that can occur. However, what would be the harm with numeric and text literals? Consider a number that plays a significant role in the implementation of...
11
2871
by: Ben Hetland | last post by:
....in certain cituations they can be useful if not for anything else, then at least for saving a lot of repetetetetetitititive typing. :-) Beyond the point of "do something better instead", I'm curious about how the following syntactical problem can be solved. It should apply equally to C and C++ as it mainly is a preprocessor-related problem. I tryed to define something similar to the following example: ...
3
2671
by: Stephen Sprunk | last post by:
On a project I'm working on, I ran across the following macros: /* assume s is struct stream *, s->p is char, v is unit16_t or uint32_t */ #define in_uint16_le(s,v) { v = *((s)->p++); v += *((s)->p++) << 8; } #define in_uint32_le(s,v) { in_uint16_le(s,v) \ v += *((s)->p++) << 16; v += *((s)->p++) << 24; } I'm personally not fond of function-like macros and wanted to turn these into static inline functions, but I'm having trouble doing...
11
22392
by: San | last post by:
hi there, I am new to c++ and tryig to learn the basics of the c++ concepts. While I was reading the templates, I realize that the templates are a syntax that the compilar expands pased upon the type specified. This is much similar like a macro expansion in C. can anyone please explain advantages of one over the other ? Thanks in advance -
33
8916
by: Robert Seacord | last post by:
When writing C99 code is a reasonable recommendation to use inline functions instead of macros? What sort of things is it still reasonable to do using macros? For example, is it reasonable to write type generic functions macros? #define CUBE(I) ( (I) * (I) * (I) ) Is there some more concise set of things where inline functions should be used instead of macros? Multi-statement macros, for example?
0
9901
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9752
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10684
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
7918
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5744
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5942
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4563
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
4159
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3188
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.