ID:               30350
 User updated by:  colin at encode dot net dot au
 Reported By:      colin at encode dot net dot au
-Status:           Open
+Status:           Bogus
 Bug Type:         Arrays related
 Operating System: Windows XP Pro SP2
 PHP Version:      5.0.2
 New Comment:

Ok, it appears that the element is created because we are attempting to
return a reference to something that does not exist.  Updating status. 
:)


Previous Comments:
------------------------------------------------------------------------

[2004-10-07 05:26:07] colin at encode dot net dot au

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 this bug report at http://bugs.php.net/?id=30350&edit=1

Reply via email to