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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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-2025 The PHP Group
All rights reserved.
Last updated: Fri Jan 03 12:01:29 2025 UTC