php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #30350 Returning reference to array element produces strange result
Submitted: 2004-10-07 05:26 UTC Modified: 2004-10-07 07:42 UTC
From: colin at encode dot net dot au Assigned:
Status: Not a bug Package: Arrays related
PHP Version: 5.0.2 OS: Windows XP Pro SP2
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: colin at encode dot net dot au
New email:
PHP Version: OS:

 

 [2004-10-07 05:26 UTC] 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.


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-10-07 07:42 UTC] colin at encode dot net dot au
Ok, it appears that the element is created because we are attempting to return a reference to something that does not exist.  Updating status.  :)
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Tue Jul 07 10:01:25 2020 UTC