致命错误:Uncaught TypeError:Class Method的返回值必须是void的实例,并且返回none

时间:2018-03-13 15:05:04

标签: php oop php-7.2

说明:必须启动游戏,然后进行多轮(必须放置playRounds(3)),并且在游戏模式调用中,玩家必须撤回并且该轮的获胜者。轮次结束后,必须有一个赢家方法,显示哪个玩家赢了。当有胜利者时,一轮结束 - 在同一手中,游戏继续在同一轮中直到赢得胜利。 要求:没有前端(游戏可以报告打印时发生的事情 - 不需要任何其他可视化)您应该可以轻松获得额外的选项来绘制不同类型的“石头,剪刀,纸”手。

奖金条件:1。已实现单元测试; 2.在作曲家包装上制作。

文件app.php

<?php

    include "entity/Game.php";
    include "entity/Player.php";

    $pavel = new Player("Pavel");
    $gosho = new Player("Gosho");

    $game = new Game([$pavel, $gosho]);
    $game->playRounds(3);
    $game->play();

    echo $game->getWinner()->getName();

    $anotherGame = new Game([$pavel, $gosho]);
    $pavel->setWeapon("paper");
    $gosho->setWeapon("stone");
    echo PHP_EOL . $anotherGame->fight()->getName();

文件Game.php

<?php

class Game
{
    /** @var Player[] */
    private $players;
    private $rounds;
    private $winner;
    private $weapons = ["stone", "scissors", "paper"];

    public function __construct(array $players)
    {
        $this->players = $players;
    }

    public function setRounds(int $rounds)
    {
        $this->rounds = $rounds;
    }

    public function getRounds(): int
    {
        return $this->rounds;
    }

    public function playRounds(int $rounds): void
    {
        $this->rounds = $rounds;
    }

    public function getWinner(): Player
    {
        return $this->winner;
    }

    public function setWinner(Player $winner): void
    {
        $this->winner = $winner;
    }

    public function play()
    {
        while ($this->getRounds() !== 0) {
            $this->players[0]->setWeapon($this->weapons[rand(0, 2)]);
            $this->players[1]->setWeapon($this->weapons[rand(0, 2)]);

            if ($this->players[0]->getWeapon() === $this->players[1]->getWeapon()) {
                continue;
            }

            $this->fight()->addScore();
            $this->setRounds($this->getRounds() - 1);
        }

        if ($this->players[0]->getScore() > $this->players[1]->getScore()) {
            $this->setWinner($this->players[0]);
        } else {
            $this->setWinner($this->players[1]);
        }
    }

    public function fight(): Player
    {

        if ($this->players[0]->getWeapon() === $this->weapons[0]) {
            if ($this->players[1]->getWeapon() === $this->weapons[1]) {
                return $this->players[0];
            }

            if ($this->players[1]->getWeapon() === $this->weapons[2]) {
                return $this->players[1];
            }
        }

        if ($this->players[0]->getWeapon() === $this->weapons[1]) {
            if ($this->players[1]->getWeapon() === $this->weapons[0]) {
                return $this->players[1];
            }

            if ($this->players[1]->getWeapon() === $this->weapons[2]) {
                return $this->players[0];
            }
        }

        if ($this->players[0]->getWeapon() === $this->weapons[2]) {
            if ($this->players[1]->getWeapon() === $this->weapons[0]) {
                return $this->players[0];
            }
        }

        return $this->players[1];
    }
}

文件Player.php

class Player
{
    private $name;
    private $score = 0;
    private $weapon;

    public function __construct(string $name)
    {
        $this->name = $name;
    }

    public function getWeapon(): string
    {
        return $this->weapon;
    }

    public function setWeapon(string $weapon): void
    {
        $this->weapon = $weapon;
    }

    public function getName(): string
    {
        return $this->name;
    }

    public function setName(string $name): void
    {
        $this->name = $name;
    }

    public function getScore(): int
    {
        return $this->score;
    }

    public function addScore(int $score = 1): void
    {
        $this->score += $score;
    }
}

代码写在php7.2上。

如何修复此致命错误?如何修复错误并解决问题?

  

致命错误:未捕获TypeError:Game :: playRounds()的返回值必须是void的实例,在D:\ XAMPP_2 \ htdocs \ php-oop \ task \ game \ entity \ Game.php中返回无效三十   TypeError:Game :: playRounds()的返回值必须是void的实例,没有在第30行的D:\ XAMPP_2 \ htdocs \ php-oop \ task \ game \ entity \ Game.php中返回

3 个答案:

答案 0 :(得分:1)

void返回类型已为implemented in PHP 7.1。 PHP 7.0将void返回类型视为类名。

确保使用PHP 7.1或更高版本运行代码。

检查代码如何使用各种PHP解释器执行,并查看PHP 7.0报告的错误消息:https://3v4l.org/Tvb6h#v700

答案 1 :(得分:1)

7.1中将

void类型添加到PHP中,但为了使其在较低版本的PHP中工作,只需删除:void,结果将是相同的。特别是因为你没有使用严格的模式。

答案 2 :(得分:0)

使用控制台检查PHP版本可能会给您带来另一个版本。使用phpinfo()函数检查您的PHP版本,并确保是否使用最新版本。

sudo a2dismod php7.0
sudo a2enmod php7.2
sudo service apache2 restart