PHP 4.0 Bug #4053: define() always returns TRUE if attempting to redefine a constant
| From: | reaster at comptechnews dot com | Date: | Wed, 05 Apr 2000 20:32:29 +0000 |
| Subject: | PHP 4.0 Bug #4053: define() always returns TRUE if attempting to redefine a constant | ||
| Groups: | php.dev | ||
| Request: | Send a blank email to php-dev+get-17652@lists.php.net to get a copy of this message | ||
From: reaster@comptechnews.com
Operating system: Linux glibc
PHP version: 4.0 Beta 4 Patch Level 1
PHP Bug Type: Misbehaving function
Bug description: define() always returns TRUE if attempting to redefine a constant
I discovered minor problems with the define() function. Please consider the following ...
When I run:
echo "\nReturned: " . define("CONSTANT", 1);
echo "\nValue: " . CONSTANT;
echo "\nReturned: " . define("CONSTANT", 2);
echo "\nValue: " . CONSTANT;
I get:
Returned: 1
Value: 1
Returned: 1
Value: 1
According to the documentation, a constant cannot be redefined.
define() doesn't violate that rule, but the return value misleads you
to think that the second define() call was a success and that the
value should be 2.
I wanted to emulate c-style #ifndef #endif conditional inclusion in my phps
include files as follows:
if(define("FILE_PHPS",1) ) {
// contents of include file, functions etc.
}
When I try this, define() returns true everytime. I thought maybe it should
return FALSE if FILE_PHPS is already defined and not include the
contents.
If I try doing it this way:
if(!defined("CONSTANT")) {
define("CONSTANT", 1);
// rest of contents of include file ,functions etc
}
then everything is fine. This looks more like how C does it.
However, it now takes two function calls instead of just one.
A php script would use my phps include file with a simple include:
include("file.phps");
Multiple occurrances of include("file.phps"); would not cause any
problem.
A final suggestion for define() is that it default to a value of 1 so that
you can just define a constant as follows:
define("CONSTANT");
this would make CONSTANT equal to 1 and case sensitive.
If there is some better way to conditionally include files like the C preprocessor does for C,
please drop me an email. Thanks.