简体   繁体   中英

Php Fatal error: Cannot pass parameter 1 by reference in /srv/www/default/work/index.php on line 30

I have a project where I need to make a dice roller class. The class is to be public with private member variables and the public member functions are only to be able to modify the member variables. The constructor takes a parameter which is the maximum number of sides on the dice to be rolled just in case this class is later used for non six sided dice rolls. It's throwwing the error in the title and I'm not sure how to fix that error. Also if I just pass the private variables by value to the member functions it doesn't uphold the variables value.

<?php
/*
 * The base code of the webGames.
 * This file has the classes for cards and dice
 * version 1.0
 * file gamesLib.php
 * build date 6/25/2015
 *
 * To run this library use an include statement to include the file,
 * or download the whole library package and run it all.
 *
 */
class GameDie
{
   private $dieRollValue = 0;
   const MINIMUM_DIE_SIDES = 1;
   private $maxDieSides = 2;

   public function __construct( &$initialMaxDieSides)
   {
        if (is_int ($initialMaxDieSides))
       {
           $maxDieSides = $initialMaxDieSides;
           unset($initialMaxDieSides);
           $this->setDieRoll();
       }
       else
       {  
           print '<script language="javascript">';
           print 'alert("Function: gamesLib did not correctly set a max die side value in the constructor.")';
           print '</script>';
        }
   }

   public function setDieMaxSides( &$passedDieMaxSidesValue)
   {
       if (is_int ($passedDieMaxSidesValue))
       {
           $maxDieSides = $passedDieMaxSidesValue;
           unset($passedDieMaxSidesValue);
       }
       else
       {  
           print '<script language="javascript">';
           print 'alert("Function: gamesLib did not correctly set a max die side value in setDieMaxSidesValue.")';
       print '</script>';
        }
   }

    public function getDieRoll()
    {
        $this->$dieRollValue;
    }

    public function setDieRoll()
    {
        $this->$dieRollValue = (mt_rand(GameDie::MINIMUM_DIE_SIDES, $maxDieSides));
    }
}
$onlyDie = new GameDie(6); 
print ($onlyDie->getDieRoll()); 
?>

The error is in your constructor:

public function __construct( &$initialMaxDieSides)

The error is occurring because you are trying to pass the number 6 to the constructor which is expecting a variable. When you use & before parameter 1, it is saying to pass by reference. The number 6 is simply a value and has no reference. You can correct this either by making the constructor accept the variable without the reference ( public function __construct( $initialMaxDieSides) ) or making the function call:

$number = 6;
$onlyDie = new GameDie($number);

When you pass GameDie($number), $number is a variable and can be passed by reference.

That will fix your first issue, but you have several other issues in your code.... but it's an assignment, so i'll let you figure all those out on your own. :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM