dongzhang7157 2011-06-16 08:05
浏览 28
已采纳

PHP引用传递使对象的新实例?

I have a ParentClass and when I make a new object I want to pass a reference to the ParentClass. (I have to use the ParentClass things in the new object)

I use the constructor to make this object and pass the reference value. (that's important for me)

But when I use the =& operator, it makes a new instance of the ParentClass, what call the constructor, and then it's fall an endless recursion.

Here's my code:

<?php

abstract class MainClass {

    function __construct(&$parent){
        $this->parent =& $parent;
        $this->init();
    }

    abstract protected function init(); 

}

class ParentClass extends MainClass {   

    protected function init(){
        $this->child = new ChildClass($this);
    }

}

class ChildClass extends MainClass {

    protected function init(){}

}


$parent = new ParentClass (new stdClass());
var_dump($parent);

?>

And the result:

object(ParentClass)#1 (2) {
  ["parent"]=>
   object(stdClass)#2 (0) {
  }
  ["child"]=>
   object(ChildClass)#3 (1) {
     ["parent"]=>
     object(ParentClass)#1 (2) {
       ["parent"]=>
       object(stdClass)#2 (0) {
       }
       ["child"]=>
       object(ChildClass)#3 (1) {
         ["parent"]=>
         *RECURSION*
       }
     }
   }
 }

How can I solve this problem?

  • 写回答

2条回答 默认 最新

  • doutang7383 2011-06-16 08:14
    关注

    Objects are passed by reference by default. There is no reason to pass or assign the $parent by reference. So this should be sufficient:

    abstract class MainClass {
    
        function __construct($parent){
            $this->parent = $parent;
            $this->init();
        }
    

    It might be important to you to use &$parent, but it is totally unnecessary.


    Regarding the recursion: There is no recursion in your code, it is recursion in the output.

    This part:

    object(ChildClass)#3 (1) {                // <--- the same element
        ["parent"]=>
        object(ParentClass)#1 (2) {
          ["parent"]=>
          object(stdClass)#2 (0) {
          }
          ["child"]=> 
          object(ChildClass)#3 (1) {          // <--- the same element
            ["parent"]=>
            *RECURSION*
          }
        }
      }
    

    would be printed over and over again, because the child has a reference to its parent and the parent a reference to its child.

    Maybe even more obvious are the repeating numbers in the output:

    object(ParentClass)#1            // 1
      object(stdClass)#2             // 2
      object(ChildClass)#3           // 3
        object(ParentClass)#1        // 1
          object(stdClass)#2         // 2
          object(ChildClass)#3       // 3
            // what would be here? right, object(ParentClass)#1 again
    

    This is normal, there is no problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改