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

发布于 2024-11-16 02:12:58 字数 1154 浏览 1 评论 0原文

我有一个 ParentClass,当我创建一个新对象时,我想传递对 ParentClass 的引用。 (我必须在新对象中使用ParentClass的东西)

我使用构造函数来创建这个对象并传递引用值。 (这对我来说很重要)

但是当我使用 =& 运算符时,它会创建一个 ParentClass 的新实例,调用构造函数,然后它就会陷入无休止的递归。

这是我的代码:

<?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);

?>

结果:

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*
       }
     }
   }
 }

我该如何解决这个问题?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一身仙ぐ女味 2024-11-23 02:12:58

默认情况下,对象通过引用传递。没有理由通过引用传递或分配 $parent。所以这应该足够了:

abstract class MainClass {

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

使用 &$parent 对您来说可能很重要,但完全没有必要。


关于递归:您的代码中没有递归,它是输出中的递归。

这部分:

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*
      }
    }
  }

将被一遍又一遍地打印,因为子级有对其父级的引用,而父级有对其子级的引用。

也许更明显的是输出中的重复数字:

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

这是正常的,没有问题。

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.

冷弦 2024-11-23 02:12:58

更好的设计。

您不需要对父类的引用。如果有方法需要类似的东西,那么它们应该是覆盖所有子对象的静态方法。

Better design.

You shouldn't need a reference to the parent class. If there are methods that need something like that, then they should be static methods that cover all child objects.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文