From:             cschneid at cschneid dot com
Operating system: 
PHP version:      6SVN-2009-10-16 (SVN)
PHP Bug Type:     Feature/Change Request
Bug description:  Strip $ at beginning of variable names for compact()

Description:
------------
One reason we are not using compact() in our production code is that it is
hard to search for variable references because $foo is simply called 'foo'
in compact()

Suggestion: Strip off a leading $ before looking up variables, i.e. make
compact('$foo') equivalent to compact('foo').

It's clear that people can shoot themselves in the foot if they try
compact("$foo") but it would possibly make code using compact() a little
easier to maintain.

Patch:
Index: ext/standard/array.c
===================================================================
--- ext/standard/array.c        (revision 289696)
+++ ext/standard/array.c        (working copy)
@@ -1488,7 +1488,7 @@
 {
        zstr key;
        int key_len;
-       zend_bool free_key = 0;
+       void *free_key = NULL;
        zval **value_ptr, *value, *data;

        if (Z_TYPE_P(entry) == IS_STRING || Z_TYPE_P(entry) == IS_UNICODE)
{
@@ -1505,9 +1505,19 @@
                        } else if (norm != key.u) {
                                key.u = norm;
                                key_len = norm_len;
-                               free_key = 1;
+                               free_key = norm;
                        }
+                       if (*key.u == '$') {
+                               key.u++;
+                               key_len--;
+                       }
                }
+               else {
+                       if (*key.s == '$') {
+                               key.s++;
+                               key_len--;
+                       }
+               }
                if (zend_u_hash_find(eg_active_symbol_table,
Z_TYPE_P(entry), key, key_len + 1, (void **)&value_ptr) != FAILURE) {
                        value = *value_ptr;
                        ALLOC_ZVAL(data);
@@ -1518,7 +1528,7 @@
                        zend_u_hash_update(Z_ARRVAL_P(return_value),
Z_TYPE_P(entry), key, key_len + 1, &data, sizeof(zval *), NULL);
                }
                if (free_key) {
-                       efree(key.v);
+                       efree(free_key);
                }
        }
        else if (Z_TYPE_P(entry) == IS_ARRAY) {


Reproduce code:
---------------
sapi/cli/php -r '$foo = "FOO"; var_dump(compact("\$foo"));'

Expected result:
----------------
array(1) {
  [u"foo"]=>
  unicode(3) "FOO"
}


Actual result:
--------------
array(0) {
}


-- 
Edit bug report at http://bugs.php.net/?id=49903&edit=1
-- 
Try a snapshot (PHP 5.2):            
http://bugs.php.net/fix.php?id=49903&r=trysnapshot52
Try a snapshot (PHP 5.3):            
http://bugs.php.net/fix.php?id=49903&r=trysnapshot53
Try a snapshot (PHP 6.0):            
http://bugs.php.net/fix.php?id=49903&r=trysnapshot60
Fixed in SVN:                        
http://bugs.php.net/fix.php?id=49903&r=fixed
Fixed in SVN and need be documented: 
http://bugs.php.net/fix.php?id=49903&r=needdocs
Fixed in release:                    
http://bugs.php.net/fix.php?id=49903&r=alreadyfixed
Need backtrace:                      
http://bugs.php.net/fix.php?id=49903&r=needtrace
Need Reproduce Script:               
http://bugs.php.net/fix.php?id=49903&r=needscript
Try newer version:                   
http://bugs.php.net/fix.php?id=49903&r=oldversion
Not developer issue:                 
http://bugs.php.net/fix.php?id=49903&r=support
Expected behavior:                   
http://bugs.php.net/fix.php?id=49903&r=notwrong
Not enough info:                     
http://bugs.php.net/fix.php?id=49903&r=notenoughinfo
Submitted twice:                     
http://bugs.php.net/fix.php?id=49903&r=submittedtwice
register_globals:                    
http://bugs.php.net/fix.php?id=49903&r=globals
PHP 4 support discontinued:          http://bugs.php.net/fix.php?id=49903&r=php4
Daylight Savings:                    http://bugs.php.net/fix.php?id=49903&r=dst
IIS Stability:                       
http://bugs.php.net/fix.php?id=49903&r=isapi
Install GNU Sed:                     
http://bugs.php.net/fix.php?id=49903&r=gnused
Floating point limitations:          
http://bugs.php.net/fix.php?id=49903&r=float
No Zend Extensions:                  
http://bugs.php.net/fix.php?id=49903&r=nozend
MySQL Configuration Error:           
http://bugs.php.net/fix.php?id=49903&r=mysqlcfg

Reply via email to