Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 构造函数导致无限循环_Php_Oop - Fatal编程技术网

Php 构造函数导致无限循环

Php 构造函数导致无限循环,php,oop,Php,Oop,我正在尝试完成,目前我有以下代码: <?php /* Challenge 2: Implement AnswerInterface and get Question to echo "4". */ class Question { public function __construct(AnswerInterface $answer) { echo "What is 2 + 2?\n"; $answer = $answer->g

我正在尝试完成,目前我有以下代码:

<?php

/*
Challenge 2: Implement AnswerInterface and get Question to echo "4".
*/  
class Question
{
    public function __construct(AnswerInterface $answer)
    { 
        echo "What is 2 + 2?\n";
        $answer = $answer->get()->the()->answer();
        if ($answer instanceof AnswerInterface) {
            echo $answer . PHP_EOL;
        }
    }   
}   

interface AnswerInterface
{   
    public function get();
    public function the();
    public function answer();
}   

class Answer implements AnswerInterface
{   
    public function get()
    {   
        return new Answer();
    }

    public function the()
    {
        return new Answer();
    }

    public function answer()
    {
        return new Answer();
    }

    public function __toString()
    {
        return '4';
    }
}

$answer = new Answer;
$question = new Question($answer);
我可以通过在代码中插入以下内容来修复错误:

public function __construct() {}

我不完全明白这是怎么回事。。。我想知道这里发生了什么。非常感谢您提供的任何解释这一工作原理的帮助。

在PHP拥有
\uu构造函数之前,您可以通过将方法命名为与类相同的名称来创建构造函数。这仍然受支持,但在PHP7中已被弃用。这意味着如果您的
Answer
类没有现代构造函数(
\uu-construct
),那么
Answer
方法将被称为构造函数。由于它返回一个
新答案
,构造函数将在新对象上被再次调用,一次又一次。你有一个无限递归,一旦内存耗尽就会抛出一个错误


我只能猜出这个练习的原因是什么。但是您也可以在
get
中返回
$this
the
answer
。如果您希望您的类支持“链接”,则通常会这样做。当然,在现实世界中,这些方法还可以做其他事情。

在PHP拥有
\uu构造之前,您可以通过将方法命名为与类相同的名称来创建构造函数。这仍然受支持,但在PHP7中已被弃用。这意味着如果您的
Answer
类没有现代构造函数(
\uu-construct
),那么
Answer
方法将被称为构造函数。由于它返回一个
新答案
,构造函数将在新对象上被再次调用,一次又一次。你有一个无限递归,一旦内存耗尽就会抛出一个错误


我只能猜出这个练习的原因是什么。但是您也可以在
get
中返回
$this
the
answer
。如果您希望您的类支持“链接”,则通常会这样做。当然,在现实世界中,这些方法也会做其他事情。

基本上你有正确的思维方式,但问题是你不需要在所有方法中创建新实例,你需要的只是返回对象的当前实例,因为你已经创建了对象答案并将其传递给问题对象,因此,正确的代码应该如下所示:

class Answer implements AnswerInterface
{

public function get()
{
    return $this;
}

public function the()
{
    return $this;
}

public function answer()
{
    return $this;
}

public function __toString()
    {
        return "4";
    }
}
您可能还想在此处阅读有关$this关键字的信息


另外,如果您在PHP5.3.3之后不使用名称空间,那么方法answer()将被视为类answer的构造函数方法(无论是小写还是大写)(或者,如果您使用PHP5.3.0-5.3.2,它将被视为构造函数)。所以在你的例子中,当你试图在实现的方法中调用new Answer()时,你也有无限循环。

基本上你有正确的思维方式,但问题是你不需要在所有方法中创建新实例,你需要的只是返回object的当前实例,因为您已经创建了对象答案并将其传递给问题对象,所以正确的代码应该如下所示:

class Answer implements AnswerInterface
{

public function get()
{
    return $this;
}

public function the()
{
    return $this;
}

public function answer()
{
    return $this;
}

public function __toString()
    {
        return "4";
    }
}
您可能还想在此处阅读有关$this关键字的信息


另外,如果您在PHP5.3.3之后不使用名称空间,那么方法answer()将被视为类answer的构造函数方法(无论是小写还是大写)(或者,如果您使用PHP5.3.0-5.3.2,它将被视为构造函数)。因此,在您的例子中,在尝试在实现的方法中调用new Answer()时,您还有无限循环。

谢谢,这很有意义!回顾过去,我不知道为什么我不首先返回
$this
,而不是创建一个新的类实例。非常感谢。谢谢,这很有道理!回顾过去,我不知道为什么我不首先返回
$this
,而不是创建一个新的类实例。非常感谢。我不知道我为什么不首先返回
$this
!谢谢你的帮助,我不知道我为什么不首先返回
$this
!谢谢你的帮助