<?//--这是我测试能否传递自身的地址。
class tree{
  var $treearray;//--随便一个变量,
  var $parent;
  function tree($treearray,$parent=null) {
    $this->treearray=$treearray;
    if(is_null($parent))
      $this->parent=&$this;//--为什么传递的是它的值,而不是引用?
    else 
      $this->parent=&$parent;
  }
}
$node=new tree("wo");
$node->parent->treearray="hoahao"; //注意这里
echo $node->parent->treearray;//--输出"wo",我想得到hoahao ,该如何改?
print_r($node);
?>
hoahaotree Object
(
    [treearray] => wo
    [parent] => tree Object
        (
            [treearray] => hoahao
            [parent] =>  *RECURSION*
        ))

解决方案 »

  1.   

    谢谢回复
    我希望通过修改$node的值而达到修改$node->parent的值,不知道能不能办到,(php 4.33)
      

  2.   

    $node和$node->parent已经是两个实例了
    你可以构造一个方法来完成
      

  3.   

    谢谢,还有一点我搞不清:就是下面这一句
    $this->parent=&$this
    为什么&$this不是对他本身的引用,加不加&效果是一样的.
      

  4.   

    这一点确实有点问题,这是php在实现时产生的问题。所以php5做了调整
      

  5.   

    ok了,php5我看了点介绍,的确做了很大的调整,至少类与数组变成了引用类型了。
    谢谢了。