Re: PHP 4.0 Bug #4053: define() always returns TRUE if attempting to redefine a constant

From: Date: Thu, 06 Apr 2000 17:39:28 +0000
Subject: Re: PHP 4.0 Bug #4053: define() always returns TRUE if attempting to redefine a constant
References: 1  Groups: php.dev 
Request: Send a blank email to php-dev+get-17683@lists.php.net to get a copy of this message
reaster@comptechnews.com wrote: > > 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 I had a look at the code dealing with constants. Here's a patch to have define() return FALSE if the constant has already been defined. With this patch applied : <?php echo "\nReturned: " . (define("CONSTANT", 1) ? 1 : 0); echo "\nValue: " . CONSTANT; echo "\nReturned: " . (define("CONSTANT", 2) ? 1 : 0) ; echo "\nValue: " . CONSTANT; ?> outputs : Returned: 1 Value: 1 Returned: 0 Value: 1 Which is more correct. Flavien Lebarbé. -- Flavien LEBARBE OPEN CARE Support for Freedom mailto:flebarbe@ocare.com http://www.ocare.com Tel:+33 141430890 Fax:+33 141430891 --- zend_builtin_functions.c.orig Wed Apr 5 12:21:39 2000 +++ zend_builtin_functions.c Thu Apr 6 17:43:17 2000 @@ -332,6 +332,7 @@ ZEND_FUNCTION(define) { zval **var, **val, **non_cs; + zval prev; int case_sensitive; zend_constant c; @@ -371,7 +372,11 @@ break; } convert_to_string_ex(var); - + if (zend_get_constant((*var)->value.str.val, (*var)->value.str.len,&prev)) { + zval_dtor(&prev); + RETURN_FALSE; + } + zval_dtor(&prev); c.value = **val; zval_copy_ctor(&c.value); c.flags = case_sensitive; /* non persistent */

« previous php.dev (#17683) next »