Hi,

Friday, May 14, 2004, 5:10:49 AM, you wrote:
VS> Hi!

VS> I found that when I try to call class method "addchild" from reference of
VS> object - php change not original object. New copy of object will created.

VS> Very strange....

VS> Example:
VS> <?
VS> class pages {
VS>  var $childs;
VS>  var $absnum;
VS>  function pages($absnum) {
VS>   $this->absnum = $absnum;
VS>  }
VS>  function addchild(&$node) {
VS>   if (!is_array($this->childs))
VS>    $this->childs = array();
VS>   $this->childs[$node->absnum] = $node;
VS>  }
VS> }

VS> $allpages = new pages(0);
VS> $allrows = array();
VS> $allrows[0] = &$allpages;

VS> $allrows[2] = &new pages(2);
$allrows[0]->>addchild($allrows[2]);

VS> $allrows[3] = &new pages(3);
$allrows[0]->>addchild($allrows[3]);

VS> $allrows[4] = &new pages(4);
$allrows[3]->>addchild($allrows[4]);

VS> // This is data of object $allrows[3]
VS> echo '<pre>';
VS> print_r($allrows[3]);

VS> echo '</pre>';

VS> // But in global array element $allrows[3] have another value
VS> echo '<pre>';
VS> print_r($allpages);
VS> echo '</pre>';
?>>

VS> Any ideas?

VS> Thanks


You need the =& in the addChild function

function addchild(&$node) {
  if (!is_array($this->childs))
   $this->childs = array();
  $this->childs[$node->absnum] =& $node;
 }

-- 
regards,
Tom

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to