473,804 Members | 3,720 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 32927
"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
2996
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
34279
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
2426
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
2819
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
7237
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
2863
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
2667
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
22385
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
8910
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
10580
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9157
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7621
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
5525
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
5652
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
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
3
2993
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.