473,839 Members | 1,468 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
47 32929

Oli Filth wrote:
function getWithDefault( &$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

Clever. Very clever.

Apr 9 '06 #41
On 09/04/2006 17:50, Oli Filth wrote:
Michael Winter wrote:


[snip]
<input name="control-name[]" ...>

?

There's nothing remotely invalid about that


Hmmm, you're absolutely correct! I could have sworn that once upon a
time I read that use of brackets in element names was invalid HTML,
and I found it to be the case running against the validator.


Perhaps you duplicated the value in an id attribute? The ID type is very
restrictive (letters, numbers, periods, colons, hyphens, and underscores).

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Apr 9 '06 #42
Tim Martin wrote:
I have a fair amount of scepticism for this way of thinking myself. But
at the same time, I think anyone who's developed software in a team
environment will understand that at least one requirement of good code
is that it be readable by other people. Constraining programming style
(whether it be with language features, formal coding standards or
informal unspoken agreement) is to some extent a necessary part of that.


I agree with that up to a point. Readability obviously aids
development. On the other hand it has come to be that some people see
it as an end in itself. With aesthetic factor as the chief motive, they
busy themselves hiding "messy" parts of their code behind layers of
abstractions. This actually ends up decreasing readability since you
can't read what you can't see.

Looking at the issue empirically, I don't see macros as troublesome.
The source codes of such major projects as Linux, Apache, and PHP
itself are littered through out with macros. Since programmer usually
use them for oft-repeated code segments, you get accustomed to the
macros after a while, even though they look highly opaque at a glance.
In my opinion C++ templates are far less readable.

Apr 9 '06 #43
Tim Martin wrote:
Jerry Stuckle wrote:
Tim Martin wrote:
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.

What "General consensus"? I haven't heard that.

For example, Sutter / Alexandrescu, "C++ Coding standards". They dismiss
macros in no uncertain terms. You could argue about quite how widely
accepted this viewpoint is (not that I have any interest in such an
argument).


That's hardly a "general consensus". It's also about C/C++, not PHP.

And it goes against virtually every "coding standard" I've ever seen in C/C++.

I deliberately phrased it like that to avoid giving the impression that
I endorse this consensus myself. Personally, I think there are plenty of
valid uses for macros in C (provided they are used with care). Whether
the benefits outweigh the costs in PHP (or indeed C++) is much less
obvious.
And just because someone might misuse them means no one can have
them? People might misuse functions - get rid of them. Someone might
misuse relational databases - get rid of them.
>

It's not a valid argument.

That wouldn't be a valid argument, but that wasn't the extent of my
argument. You have to weigh up the potential benefits against costs. The
fact that inexperienced programmers might end up using them to create
unmaintainable code is a very definite cost. So far I haven't seen any
real benefits.


The potential benefits of having them are that they can make coding more
understandable and easier to maintain.

The lack of macros doesn't give you the option.

Out of interest, do you think that register_global s should be enabled by
default, since it causes no problems for people who write correct code?


An entirely different discussion - which I am not going to get into here.
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.

Make code clearer, more concise and more maintainable. All of which
can be increased by the judicious use of macros.

The question is whether the same thing can be achieved by judicious use
of existing constructs, without having to introduce macros into the
language. So far, I haven't seen any proof otherwise.

Tim


No it can't. Read my other posts.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 9 '06 #44
Chung Leong wrote:
Tim Martin wrote:

In my opinion C++ templates are far less readable.


Amen! Even though I am a heavy user of C++ templates and do my best to make
them clearer.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 9 '06 #45
Csaba Gabor wrote:
3. Do you really want to write a parser? I'd rather not. But now one
must be aware that macro definitions within strings will also be
expanded.


One does not need to write full-blown parser to protect strings and
other lexical elements where partial tokeniser will do the job just fine.

Roman
Apr 10 '06 #46
Jerry Stuckle wrote:
Tim Martin wrote:
Jerry Stuckle wrote:
Tim Martin wrote:

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.
What "General consensus"? I haven't heard that.
For example, Sutter / Alexandrescu, "C++ Coding standards". They
dismiss macros in no uncertain terms. You could argue about quite how
widely accepted this viewpoint is (not that I have any interest in
such an argument).


That's hardly a "general consensus".


I quoted that as one example. As I say, I don't have any particular
interest in arguing over how widely shared this opinion is. I'll gladly
modify my original statement to "some people believe that..." if it
makes you happier.
It's also about C/C++, not PHP.
My original comment was about usage of macros in languages that have
them (i.e. C and C++), so this is an appropriate example. I'm obviously
not going to find any coding standards that forbid using macros in PHP.

For what it's worth, I've spent a lot of time writing C and I've used
macros extensively. When used properly I believe they can make code much
easier to maintain. However, I haven't found the same need in PHP.
The potential benefits of having them are that they can make coding more
understandable and easier to maintain.


So you say. I'm yet to see any real examples of situations in PHP where
macros give a significant benefit and similar behaviour couldn't be
achieved either by using an existing language construct, or by making a
small but less significant change to the language.

Tim
Apr 10 '06 #47
Tim Martin wrote:
Jerry Stuckle wrote:
Tim Martin wrote:
Jerry Stuckle wrote:

Tim Martin wrote:

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

What "General consensus"? I haven't heard that.
For example, Sutter / Alexandrescu, "C++ Coding standards". They
dismiss macros in no uncertain terms. You could argue about quite how
widely accepted this viewpoint is (not that I have any interest in
such an argument).

That's hardly a "general consensus".

I quoted that as one example. As I say, I don't have any particular
interest in arguing over how widely shared this opinion is. I'll gladly
modify my original statement to "some people believe that..." if it
makes you happier.
It's also about C/C++, not PHP.

My original comment was about usage of macros in languages that have
them (i.e. C and C++), so this is an appropriate example. I'm obviously
not going to find any coding standards that forbid using macros in PHP.

For what it's worth, I've spent a lot of time writing C and I've used
macros extensively. When used properly I believe they can make code much
easier to maintain. However, I haven't found the same need in PHP.
The potential benefits of having them are that they can make coding
more understandable and easier to maintain.

So you say. I'm yet to see any real examples of situations in PHP where
macros give a significant benefit and similar behaviour couldn't be
achieved either by using an existing language construct, or by making a
small but less significant change to the language.

Tim


Again - read back through other posts in this thread.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 10 '06 #48

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

Similar topics

21
3000
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
34360
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
2430
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
2822
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
7242
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
2867
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
2670
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
22388
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
8915
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
9696
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
10903
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...
0
10290
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
7015
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5681
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
5865
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4482
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
4063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3131
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.