Possible Bug: Why is the object destroyed?

From: Date: Fri, 24 Aug 2001 20:11:24 +0000
Subject: Possible Bug: Why is the object destroyed?
Groups: php.dev 
Request: Send a blank email to php-dev+get-64277@lists.php.net to get a copy of this message
Why does the following result in the object $test->elements['one'] being wiped out? Although, I could possibly understand it remaining in it's original state if get_element() is actually returning a copy and not a reference to the object, I just don't understand why the object is destroyed. Seems like a possible bug in references / garbage collection. Tested with PHP 4.0.6 on linux. <?php class app { var $elements = array(); function add_element($name) {
      $this->elements[$name] = new element();
} function &get_element($name) {
      //$ref = &$this->elements[$name]; // uncomment and it works as expected
      return( $this->elements[$name] );
} } class element { var $values = array(); function add_values($value) {
      if (is_array($value))
      {
         foreach ($value as $one)
         {
            $this->add_values($one);
         }
      }
      else
      {
         $this->values[] = $value;
      }
} } echo "<pre>\n"; $test = new app; $test->add_element('one'); echo "\ntest:\n"; print_r($test); $el = &$test->get_element('one'); $el->add_values( array('v1', 'v2', 'v3') ); echo "\ntest:\n"; print_r($test); echo "\nel:\n"; print_r($el); echo "\n</pre>"; /* output ========== test: app Object (
    [elements] => Array
        (
            [one] => element Object
                (
                    [values] => Array
                        (
                        )
                )
        )
) test: app Object (
    [elements] => Array
        (
            [one] =>
        )
) el: element Object (
    [values] => Array
        (
            [0] => v1
            [1] => v2
            [2] => v3
        )
) */ ?>

« previous php.dev (#64277) next »