From:             colin at encode dot net dot au
Operating system: Windows XP Pro SP2
PHP version:      5.0.2
PHP Bug Type:     Arrays related
Bug description:  Returning reference to array element produces strange result

Description:
------------
I'm not entirely sure if this is a bug or not, but it seems very odd
nonetheless.  I have an array of attributes as a protected class property,
and some simple functions to manipulate this array:

<?php

class Test
{
   protected $attributes;

   public function __construct()
   {
      $this->attributes = array();
   }

   public function setAttribute($name=null,$value=null)
   {
      $this->attributes[$name] = $value;
   }

   public function getAttribute($name=null)
   {
      return $this->attributes[$name];
   }
}

$test = new Test();

$test->setAttribute('foo','bar');
$test->setAttribute('omg','bbq');

echo $test->getAttribute('foo');
echo $test->getAttribute('eep');

print_r($test);

?>

Upon executing this code we should define two elements in the attributes
array, show the output of one, then generate a notice error because the
index 'eep' does not exist, and then receive a dump of the $test object,
which should look like this:

Test Object
(
    [attributes:protected] => Array
        (
            [foo] => bar
            [omg] => bbq
        )

)

This is all fine and to be expected.  Now typically with code like this in
PHP4, I would have used an ampersand in front of the getAttribute function
definition to allow a reference to an attribute array element to be
returned.  To my understanding only objects are *always* passed around by
reference in PHP5, everything else is still copied (though I may be
wrong), so that would seem to imply to me that we still need the ampersand
to allow a reference to be returned.  So let's see what happens when we put
an ampersand in front of the getAttribute function definition above, like
so:

public function &getAttribute($name=null)
{
   return $this->attributes[$name];
}

Ok, upon execution now, I receive *no* notice error that the index 'eep'
does not exist - instead, a new null element is added to the array mapped
to the key 'eep'.  The print_r($test) now shows:

Test Object
(
    [attributes:protected] => Array
        (
            [foo] => bar
            [omg] => bbq
            [eep] => 
        )

)

What gives?  Am I doing something really stupid?  I don't understand this.


-- 
Edit bug report at http://bugs.php.net/?id=30350&edit=1
-- 
Try a CVS snapshot (php4):   http://bugs.php.net/fix.php?id=30350&r=trysnapshot4
Try a CVS snapshot (php5.0): http://bugs.php.net/fix.php?id=30350&r=trysnapshot50
Try a CVS snapshot (php5.1): http://bugs.php.net/fix.php?id=30350&r=trysnapshot51
Fixed in CVS:                http://bugs.php.net/fix.php?id=30350&r=fixedcvs
Fixed in release:            http://bugs.php.net/fix.php?id=30350&r=alreadyfixed
Need backtrace:              http://bugs.php.net/fix.php?id=30350&r=needtrace
Need Reproduce Script:       http://bugs.php.net/fix.php?id=30350&r=needscript
Try newer version:           http://bugs.php.net/fix.php?id=30350&r=oldversion
Not developer issue:         http://bugs.php.net/fix.php?id=30350&r=support
Expected behavior:           http://bugs.php.net/fix.php?id=30350&r=notwrong
Not enough info:             http://bugs.php.net/fix.php?id=30350&r=notenoughinfo
Submitted twice:             http://bugs.php.net/fix.php?id=30350&r=submittedtwice
register_globals:            http://bugs.php.net/fix.php?id=30350&r=globals
PHP 3 support discontinued:  http://bugs.php.net/fix.php?id=30350&r=php3
Daylight Savings:            http://bugs.php.net/fix.php?id=30350&r=dst
IIS Stability:               http://bugs.php.net/fix.php?id=30350&r=isapi
Install GNU Sed:             http://bugs.php.net/fix.php?id=30350&r=gnused
Floating point limitations:  http://bugs.php.net/fix.php?id=30350&r=float
MySQL Configuration Error:   http://bugs.php.net/fix.php?id=30350&r=mysqlcfg

Reply via email to