#10801 [Com]: Variable Variables and Constants
| From: | Firewall-Free48 at hotmail dot com | Date: | Tue, 20 Jul 2004 15:03:08 +0000 |
| Subject: | #10801 [Com]: Variable Variables and Constants | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-62866@lists.php.net to get a copy of this message | ||
ID: 10801
Comment by: Firewall-Free48 at hotmail dot com
Reported By: mstearne at entermix dot com
Status: Bogus
Bug Type: Variables related
Operating System: Linux 2.2
PHP Version: 4.0.4
New Comment:
<a href=http://www.geocities.com/hplxewoem895/>Free
Firewall</a>
Previous Comments:
------------------------------------------------------------------------
[2001-05-10 19:26:08] mstearne at entermix dot com
Cool. That's it. Hmm, maybe you should classify this bug
as RTFM. :-)
------------------------------------------------------------------------
[2001-05-10 19:19:12] cynic@php.net
there _is_ such an interface:
http://php.net/manual/en/function.constant.php
------------------------------------------------------------------------
[2001-05-10 19:13:10] mstearne at entermix dot com
I'm not trying to change the constant value, but there
should be an interface for referencing constants similar to
how variables can be added.
If I have constants called: MY_CONSTANT_1 and MY_CONSTANT_2
I should be about to form a variable that reference those
names like:
define("MY_CONST_1","database1");
define("MY_CONST_2","database2");
$prefix="MY_CONST_";
$whichConst="1";
$thisConst=$prefix.$whichConst."<BR>";
print $$thisConst;
$whichConst="2";
$thisConst=$prefix.$whichConst;
print $$thisConst;
That would output:
database1
database2
------------------------------------------------------------------------
[2001-05-10 18:56:56] cynic@php.net
it's variable variables, not variable constants.
what bug are you reporting? I haven't noticed.
------------------------------------------------------------------------
[2001-05-10 18:11:18] mstearne at entermix dot com
Variable variables techniques do not work when one of the
"variables" is a constant. The example below illustrates
this. This is probably the desired behavior for constants,
but was confusing for me when I was trying to figure it out.
The alternative I used was to add the variables I needed to
the $GLOBALS array instead of defining them as constants.
<?php
define("DB_X_NAME","database1");
define("DB_Y_NAME","database2");
$DB_Z_NAME="database3";
function connectTo($databaseName){
global $DB_Z_NAME;
$fullDatabaseName="DB_".$databaseName."_NAME";
return "\"".${$fullDatabaseName}."\"";
}
print "DB_X_NAME is ".connectTo("X")."
";
print "DB_Y_NAME is ".connectTo("Y")."
";
print "DB_Z_NAME is ".connectTo("Z")."
";
?>
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=10801&edit=1