【问题标题】:Fatal error: Uncaught TypeError: Return value of Class Method must be an instance of void, none returned致命错误:未捕获的TypeError:类方法的返回值必须是void的实例,没有返回
【发布时间】:2018-08-21 21:45:09
【问题描述】:

描述:游戏必须开始,然后进行若干回合(必须放置 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上编写的。

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

致命错误: Uncaught TypeError: Return value of Game::playRounds() must be an instance of void, none return in D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php 上线30 TypeError:Game::playRounds()的返回值必须是void实例,D:\XAMPP_2\htdocs\php-oop\task\game\entity\Game.php第30行没有返回

【问题讨论】:

  • 你确定这是在 7.2 上运行的吗?你更有可能在 PHP 7.0 上运行它
  • 错误信息有用。你用谷歌搜索了错误吗?另外,第 30 行是哪一行?它准确地告诉您问题出在哪里(老实说,问题是什么)。
  • @JonStirling - 据我所知,是什么让你觉得它是 7.0 而不是 7.2?
  • 这正是您在 7.0 上运行时看到的完全。 void 返回类型提示是在 7.1 中添加的——在此之前它被解释为类名。见3v4l.org/X5OZl
  • 要让它在 PHP(支持)版本 5 中运行,您需要删除返回类型提示和标量类型提示。

标签: php oop php-7.2


【解决方案1】:

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

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

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

【讨论】:

  • 我的 PHP 版本 7.0.13,在较新的版本上不会出错。如何使用 PHP 7.0.13 版。
  • 要使其与 PHP 7.0 一起使用,请删除有问题的方法的 void 返回类型。要使其与 PHP 5 一起使用,请删除所有 return types 和标量 parameter typesintstring)。
  • 是的,我明白了,我明白了。非常感谢您的帮助。 (y) :)
【解决方案2】:

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

【讨论】:

  • 非常感谢您的帮助。 (y) :) :)
【解决方案3】:

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

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

【讨论】:

    猜你喜欢
    • 2019-07-18
    • 1970-01-01
    • 2018-10-18
    • 2017-04-22
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 2015-06-29
    • 2014-04-15
    相关资源
    最近更新 更多