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
         )

)

*/
?>


-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]

Reply via email to