PHP 引用传递创建对象的新实例?
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,对象通过引用传递。没有理由通过引用传递或分配
$parent
。所以这应该足够了:使用
&$parent
对您来说可能很重要,但完全没有必要。关于递归:您的代码中没有递归,它是输出中的递归。
这部分:
将被一遍又一遍地打印,因为子级有对其父级的引用,而父级有对其子级的引用。
也许更明显的是输出中的重复数字:
这是正常的,没有问题。
Objects are passed by reference by default. There is no reason to pass or assign the
$parent
by reference. So this should be sufficient: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:
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:
This is normal, there is no problem.
更好的设计。
您不需要对父类的引用。如果有方法需要类似的东西,那么它们应该是覆盖所有子对象的静态方法。
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.