【问题标题】:Fatal error: Uncaught Error: Call to a member function getFaceValue() on null [closed]致命错误:未捕获的错误:在 null 上调用成员函数 getFaceValue() [关闭]
【发布时间】:2021-12-18 12:34:29
【问题描述】:

我刚开始使用 php oop 并遇到了错误。我似乎无法自己找到解决方案。 错误是:

致命错误:未捕获的错误:调用 C:\xampp\htdocs\dicegame\Game.php:26 中的成员函数 getFaceValue() 为 null 堆栈跟踪:#0 C:\xampp\htdocs\dicegame\index。 php(34): Game->play('3') #1 {main} 在第 26 行的 C:\xampp\htdocs\dicegame\Game.php 中抛出。

如果您知道答案或有任何提示,请告诉我!

index.php

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <title>Dice Game </title>
        <link rel="stylesheet" type="text/css" href="css/style.css">
    </head>

    <body>
        <div id="container">    
            <header>
                <?php
                ?>
            </header>

            <form action="index.php" method="POST">
                <input type="number" name="quantity" value="3" id="quantity" min="3" max="8" placeholder="Number of dice"></br>
                <input type="submit" name="throw" id="throwBtn" value="Roll">
            </form>


            <?php 
                include('NumberToThrow.php');
                include('Dice.php');
                include('Game.php');

                $quantity = $_POST["quantity"] ?? "";

                // start the game
                if(isset($_POST['throw'])) {

                    echo "<h3>Throw the dices, baby ................. <br/></h3>";
                    $myGame = new Game();
                    $myGame->play($quantity);
                }

            ?>
        </div>
        <footer ></footer>
    </body>
</html>
game.php

<?php 
    class Game {
        // class variables
        private $diceValue;         
        private $throwNumber;       
        private $count;
        private $dicesArray;    


        public function play($quantity) {
            
            $this->createThrowNumber();
            $this->count = 0;
            
            $this->dicesArray = new SplFixedArray(8);       // create array with length initially equals to 8
            for ($index = 0; $index < $quantity; $index++)
            {
                $dice = new Dice();                             // instantiate dice object
                $dice->throwDice();                             // make the throw
                $this->dicesArray[$index] = $dice;
            }
        
            foreach($this->dicesArray as $dice) // The code in this foreach could also be implemented in
                                                // the for-loop above, but for example these are seperated
            {           
                $this->diceValue = $dice->getFaceValue();       // get the result of the throw & assign it to $diceValue
                echo("</br>");
                echo "The value of the dice: " . $this->diceValue . " ";
                $this->showDiceImage($this->diceValue);         // show value of throw as an image 
                echo("</br>");
                if ($this->diceValue == $this->throwNumber) $this->count++;
            }

            
            // display statistics
            if($this->count > 0) {
                echo "</br>Number thrown: " . $this->count . " times.</br>";
            }
            else {
                echo "</br>Number is not thrown.</br>";
            }
        }


        public function createThrowNumber() {
            $numberObject = new NumberToThrow();    // instantiate object - instance of the NumberToThrow class
            $this->throwNumber = $numberObject->makeAThrow(); // make a number to throw
            echo "<p>Your number to throw: <b>" . $this->throwNumber . "</b></p>";
            $this->showDiceImage($this->throwNumber);   // show the number to throw as dice image
            echo "</br>";
        }


        public function showDiceImage($dots) {
            switch($dots) {
                case 1:
                    echo "<img class='dotsImg' src='images/d1.png'>";
                    break;
                case 2:
                    echo "<img class='dotsImg' src='images/d2.png'>";
                    break;
                case 3:
                    echo "<img class='dotsImg' src='images/d3.png'>";
                    break;
                case 4:
                    echo "<img class='dotsImg' src='images/d4.png'>";
                    break;
                case 5:
                    echo "<img class='dotsImg' src='images/d5.png'>";
                    break;
                case 6:
                    echo "<img class='dotsImg' src='images/d6.png'>";
                    break;
                default:
                    break;
            }
        }

    }
?>
Dice.php

<?php 
    class Dice {
        const NUMBER_OF_SIDES = 6;
        private $faceValue;

        public function throwDice() {
            $this->faceValue = rand(1, self::NUMBER_OF_SIDES);  // 1 & NUMBER_OF_SIDES are inclusive values
        }

        public function getFaceValue() {
            return $this->faceValue;
        }
    }
    
?>

【问题讨论】:

  • 你为什么要使用SplFixedArray,如果很明显你的数组没有固定数量的元素?如果你这样做了,请使用$quantity 来初始化SplFixedArray($quantity),这样你的数组就只有你期望的大小,而不是你可以迭代的空。

标签: php oop


【解决方案1】:

当您创建 $this-&gt;dicesArray = new SplFixedArray(8); 时,它已经是一个包含 8 个索引值 NULL 的数组,如果您没有用您的对象填充所有索引,那么可以说 public function play($quantity) 是用 $quantity = 6 调用的,所以结果,您将拥有最后一个值为 NULL https://i.imgur.com/CWAtSlZ.png 的数组。希望现在您知道如何解决它。

【讨论】:

    猜你喜欢
    • 2017-07-13
    • 2020-10-04
    • 2016-04-20
    • 2020-08-24
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 2018-10-11
    • 2018-09-10
    相关资源
    最近更新 更多