Hello,
What is the chance of support for constants in double-quoted strings
in PHP 5.3 or PHP 6.0?
This is something I have wanted for some time now.
Use cases:
Writing an SQL query that makes use of a status field
<?php
define ('STATUS_ACTIVE', 1);
define ('NUM_PER_PAGE', 25);
$q = "SELECT ID, Name FROM Projects WHERE StatusID = " . STATUS_ACTIVE
. " LIMIT " . NUM_PER_PAGE . " OFFSET {$offset}";
$res = mysql_query ($q);
?>
Syntax suggestions:
Constants would need to be contained within curly braces. e.g.
"SELECT ID, Name FROM Projects WHERE StatusID = {STATUS_ACTIVE} LIMIT
{NUM_PER_PAGE} OFFSET {$offset}"
The syntax would therefore be:
'{', one or more of [-_a-zA-Z0-9], '}'
If not constant is found, the string should be inserted directly.
If that causes too much parsing issues, perhaps a symbol should be
placed before or after the opening brace. e.g.
"SELECT ID, Name FROM Projects WHERE StatusID = {#STATUS_ACTIVE} LIMIT
#{NUM_PER_PAGE} OFFSET {$offset}"
Regards,
Josh
People, when you will start to learn that such approach is a mess?
First, use ' ' instead of " " and format your SQL better and you woun't have
any problems:
<?php
define ('STATUS_ACTIVE', 1);
define ('NUM_PER_PAGE', 25);
// I beleve here is a lot of code
// ...
// ....
$q = 'SELECT
ID, Name
FROM Projects
WHERE StatusID = '.STATUS_ACTIVE.'
LIMIT '.NUM_PER_PAGE.', '.$offset;
$res = mysql_query ($q);
?>
a). It's more readable without syntax highlighting
b). It's just faster.
c). It's a good style.
d). I think if that is easy to do, it would be implemented a long time ago.
2008/10/29 Josh josh.sickmate@gmail.com
Hello,
What is the chance of support for constants in double-quoted strings
in PHP 5.3 or PHP 6.0?This is something I have wanted for some time now.
Use cases:
Writing an SQL query that makes use of a status field
<?php
define ('STATUS_ACTIVE', 1);
define ('NUM_PER_PAGE', 25);
$q = "SELECT ID, Name FROM Projects WHERE StatusID = " . STATUS_ACTIVE
. " LIMIT " . NUM_PER_PAGE . " OFFSET {$offset}";
$res = mysql_query ($q);
?>Syntax suggestions:
Constants would need to be contained within curly braces. e.g.
"SELECT ID, Name FROM Projects WHERE StatusID = {STATUS_ACTIVE} LIMIT
{NUM_PER_PAGE} OFFSET {$offset}"
The syntax would therefore be:
'{', one or more of [-_a-zA-Z0-9], '}'
If not constant is found, the string should be inserted directly.
If that causes too much parsing issues, perhaps a symbol should be
placed before or after the opening brace. e.g.
"SELECT ID, Name FROM Projects WHERE StatusID = {#STATUS_ACTIVE} LIMIT
#{NUM_PER_PAGE} OFFSET {$offset}"Regards,
Josh
W liście Arvids Godjuks z dnia środa 29 października 2008:
People, when you will start to learn that such approach is a mess?
First, use ' ' instead of " " and format your SQL better and you woun't
have any problems:<?php
define ('STATUS_ACTIVE', 1);
define ('NUM_PER_PAGE', 25);
$q = 'SELECT
ID, Name
FROM Projects
WHERE StatusID = '.STATUS_ACTIVE.'
LIMIT '.NUM_PER_PAGE.', '.$offset;
$res = mysql_query ($q);
?>
It's almost the same as original poster's version (except for $offset).
a). It's more readable without syntax highlighting
b). It's just faster.
c). It's a good style.
d). I think if that is easy to do, it would be implemented a long time ago.
With regard to a and c - this is just your opinion, others might find it less
readable (or indifferent). And for b - speed difference between ' and "
should be negligable (esp. with opcode caches).
I only worry it could break BC - people might have used "{SOMETEXT}" in
strings and not expect it to be interpolated (I've done so myself).
--
Paweł Stradomski
I only worry it could break BC - people might have used "{SOMETEXT}" in
strings and not expect it to be interpolated (I've done so myself).
I've done that a lot, and I've seen quite a bit of templating code that
does the same.
My personal opinion is that interpolating constants would just lead to
trouble, one way or another. Even something like "{#SOMETEXT}" could be
problematic. I would definitely discourage interpolation without some
sort of sigil though... barewords (in brackets or not) are too likely to
break BC.
Dave
Dave, how is a variable name any less a bareword than a constant name?
Thats what the backets were for, perhaps combined with a symbol to
make it even less likely, and of course if the constant is not found
in the symbol table, the constant name would be outputted directly.
I only worry it could break BC - people might have used "{SOMETEXT}" in
strings and not expect it to be interpolated (I've done so myself).I've done that a lot, and I've seen quite a bit of templating code that
does the same.My personal opinion is that interpolating constants would just lead to
trouble, one way or another. Even something like "{#SOMETEXT}" could be
problematic. I would definitely discourage interpolation without some
sort of sigil though... barewords (in brackets or not) are too likely to
break BC.Dave
Josh wrote:
Dave, how is a variable name any less a bareword than a constant name?
A bareword (in my opinion) is a word without a sigil (e.g. a leading $),
so the way that you would write a constant normally in PHP. What I was
trying to say is that having bareword-style interpolation ("{MYCONST}")
is much more likely to break BC than the "{#MYCONST}" style.
Thats what the backets were for, perhaps combined with a symbol to
make it even less likely, and of course if the constant is not found
in the symbol table, the constant name would be outputted directly.
But would it raise a warning, as PHP currently does for unrecognised
constants? If not, it could be argued that this would make subtle typos
harder to spot.
Dave