473,839 Members | 1,464 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
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.


Personally I really dislike this brief in the CS circle that somehow
programmers need to be saved from themselves. It arises from the same
elitist, I-know-what's-best-for-you mentality prevalent in academia.
More than just a tool, these ivory tower geeks want a computer language
to enforce their values and way of thinking.

[end of rant]

C style preprocessing could come in very handy in solving compatibility
issues. Anyone who has had to get a PHP5 class to work in PHP 4 knows
what I mean. A couple #define's would have taken care of the syntatical
differences, which can now only be resolved with some ugly eval()
statements.

Apr 8 '06 #21
Oli Filth wrote:
Jerry Stuckle said the following on 08/04/2006 16:11:
Tim Martin wrote:

function get_post_var_wi th_default($nam e, $default = 'default value')
{
if (isset($_POST[$name]))
{
return $_POST[$name];
}
else
{
return $default;
}
}

then later

$var = get_post_var_wi th_default('pos tvar');

Am I missing something here?

> And you'd need another one for $_SESSION, and another one for $_GET.....


And what if you want to do it for a non-super global - i.e. an array
member?

With macros you need only one, i.e.

$var = GET_WITH_DEFAUL T($_POST['myvar']), 'default value');
$var2 = GET_WITH_DEFAUL T_VALUE($myarra y['test'], 'default_value) ;

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


And if $array is empty you get a warning.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 8 '06 #22
Jerry Stuckle said the following on 08/04/2006 21:51:
Oli Filth wrote:
function getWithDefault( &$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}


And if $array is empty you get a warning.


You do? I don't. Nor can I see any reason why one should get a
warning. What version of PHP are you running?
--
Oli
Apr 8 '06 #23
Oli Filth wrote:
Jerry Stuckle said the following on 08/04/2006 21:51:
Oli Filth wrote:
function getWithDefault( &$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}


And if $array is empty you get a warning.


You do? I don't. Nor can I see any reason why one should get a
warning. What version of PHP are you running?


Try turning on all warnings.

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 9 '06 #24
Jerry Stuckle said the following on 09/04/2006 02:53:
Oli Filth wrote:
Jerry Stuckle said the following on 08/04/2006 21:51:
Oli Filth wrote:

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

And if $array is empty you get a warning.


You do? I don't. Nor can I see any reason why one should get a
warning. What version of PHP are you running?


Try turning on all warnings.

I have error level set to E_ALL | E_STRICT. The following code executes
fine:
<?php

function getValueWithDef ault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

$var = array();
echo getValueWithDef ault($var, "Roger", "Dodger") . "\n";

?>

I see no reason why an error/warning should get thrown. isset() simply
looks for an element with the specified key name in the associative
array. In an empty array, the key doesn't exist, so isset() returns false.

--
Oli
Apr 9 '06 #25
Oli Filth wrote:
Jerry Stuckle said the following on 09/04/2006 02:53:
Oli Filth wrote:
Jerry Stuckle said the following on 08/04/2006 21:51:

Oli Filth wrote:

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

And if $array is empty you get a warning.
You do? I don't. Nor can I see any reason why one should get a
warning. What version of PHP are you running?


Try turning on all warnings.


I have error level set to E_ALL | E_STRICT. The following code executes
fine:
<?php

function getValueWithDef ault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

$var = array();
echo getValueWithDef ault($var, "Roger", "Dodger") . "\n";

?>

I see no reason why an error/warning should get thrown. isset() simply
looks for an element with the specified key name in the associative
array. In an empty array, the key doesn't exist, so isset() returns false.

That's not what I said. I said if $var is EMPTY.

Take out the $var=array() line and see what you get.
--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 9 '06 #26
Jerry Stuckle said the following on 09/04/2006 06:14:
Oli Filth wrote:
I have error level set to E_ALL | E_STRICT. The following code
executes fine:
<?php

function getValueWithDef ault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

$var = array();
echo getValueWithDef ault($var, "Roger", "Dodger") . "\n";

?>

I see no reason why an error/warning should get thrown. isset()
simply looks for an element with the specified key name in the
associative array. In an empty array, the key doesn't exist, so
isset() returns false.

That's not what I said. I said if $var is EMPTY.


From the PHP manual for empty():

"The following things are considered to be empty:
...
array() (an empty array)
..."

Take out the $var=array() line and see what you get.


So you mean "undefined" ? Going back to the original purpose of this
function/macro, why would you ever want to call it on an undefined array?

But anyway, removing the $var = array() line does not result in any
error/warning. Because it's being passed by reference, PHP
automatically creates a variable $var in the global scope (because for
all it knows, this could be an output argument for the function).
--
Oli
Apr 9 '06 #27
Oli Filth said the following on 09/04/2006 13:44:
Jerry Stuckle said the following on 09/04/2006 06:14:
Oli Filth wrote:
I have error level set to E_ALL | E_STRICT. The following code
executes fine:
<?php

function getValueWithDef ault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

$var = array();
echo getValueWithDef ault($var, "Roger", "Dodger") . "\n";

?>

Take out the $var=array() line and see what you get.


But anyway, removing the $var = array() line does not result in any
error/warning. Because it's being passed by reference, PHP
automatically creates a variable $var in the global scope


That should be *caller* scope...
--
Oli
Apr 9 '06 #28
Oli Filth wrote:
Jerry Stuckle said the following on 09/04/2006 06:14:
Oli Filth wrote:
I have error level set to E_ALL | E_STRICT. The following code
executes fine:
<?php

function getValueWithDef ault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

$var = array();
echo getValueWithDef ault($var, "Roger", "Dodger") . "\n";

?>

I see no reason why an error/warning should get thrown. isset()
simply looks for an element with the specified key name in the
associative array. In an empty array, the key doesn't exist, so
isset() returns false.

That's not what I said. I said if $var is EMPTY.

From the PHP manual for empty():

"The following things are considered to be empty:
...
array() (an empty array)
..."


Yes, the array is empty. But $var is not! It contains an empty array.

Two different things.
Take out the $var=array() line and see what you get.

So you mean "undefined" ? Going back to the original purpose of this
function/macro, why would you ever want to call it on an undefined array?


Because it may or may not exist, that's why.
But anyway, removing the $var = array() line does not result in any
error/warning. Because it's being passed by reference, PHP
automatically creates a variable $var in the global scope (because for
all it knows, this could be an output argument for the function).


Yep, and now you have changed the variable, haven't you?

--
=============== ===
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attgl obal.net
=============== ===
Apr 9 '06 #29
Jerry Stuckle wrote:
Oli Filth wrote:
Jerry Stuckle said the following on 09/04/2006 06:14:
Oli Filth wrote:

I have error level set to E_ALL | E_STRICT. The following code
executes fine:
<?php

function getValueWithDef ault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}

$var = array();
echo getValueWithDef ault($var, "Roger", "Dodger") . "\n";

?>

I see no reason why an error/warning should get thrown. isset()
simply looks for an element with the specified key name in the
associative array. In an empty array, the key doesn't exist, so
isset() returns false.
That's not what I said. I said if $var is EMPTY.

From the PHP manual for empty():

"The following things are considered to be empty:
...
array() (an empty array)
..."


Yes, the array is empty. But $var is not! It contains an empty array.


No, $var doesn't *contain* an empty array, $var *is* an empty array.

<?php
$var = array();
echo empty($var) ? "TRUE" : "FALSE"; // echoes "TRUE"
?>

Take out the $var=array() line and see what you get.


So you mean "undefined" ? Going back to the original purpose of this
function/macro, why would you ever want to call it on an undefined array?

Because it may or may not exist, that's why.


I thought the purpose was to check for the existence of an *entry* in
an associative array, such as $_POST, etc.

On the other hand, checking for the existence of a *variable* implies
bad practice. There should be no need. You should always know a
priori what variables exist at any given point in your code (assuming
the wonders of register_global s are disabled).

But anyway, removing the $var = array() line does not result in any
error/warning. Because it's being passed by reference, PHP
automatically creates a variable $var in the global scope (because for
all it knows, this could be an output argument for the function).


Yep, and now you have changed the variable, haven't you?


Only if it was undefined in the first place, which would be bad
practice.

--
Oli

Apr 9 '06 #30

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
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
10584
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
10645
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
9425
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
7827
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
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...
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.