flicher88 flicher88 - 1 year ago 45
PHP Question

Access the random value of parent class

So, let's say I have this dummy code in one file:

<?php
class Dice
{
public $maxPossibleNo;
public $secretNumber;

function __construct($no_of_dice=1)
{
// how many possible dice number to enroll
$this->maxPossibleNo = $no_of_dice * 6;

// do shaking dice
$this->secretNumber = $this->getSecretNumber();
}

function getSecretNumber()
{
return rand(1, $this->maxPossibleNo);
}

function roll()
{
$found = false;
list($array1, $array2) = array_chunk(range(1, $this->maxPossibleNo), ceil($this->maxPossibleNo/2));
/*
Array
(
[0] => 1
[1] => 2
[2] => 3
)
Array
(
[0] => 4
[1] => 5
[2] => 6
)
*/

$guess = new Guess();
echo $this->secretNumber;
echo $guess->checkSecretNumber();
}
}

class Guess extends Dice
{
function checkSecretNumber()
{
return $this->secretNumber;
}

function isGreaterThan($x)
{
return $x > $this->secretNumber;
}

function isLessThan($x)
{
return $x < $this->secretNumber;
}

function isEqual($x)
{
return $x == $this->secretNumber;
}
}

$game = new Dice('1');
$game->roll();


Result:

65
43
54


Expected Result:

66
33
55


I want the Guess class to be able to access the secret number of the
Dice
class without having to roll it again. So I can manipulate the
Guess
class with other function.

EDIT:


Expectation flow: The main class will only called once (to generate
the secret number), while i will need to do loop checking for the
secret number for many times. (I guess the way is to create another
class of it, and it will be able to be called repeatly for auto
checking purpose, but i had did mistake here and dont have any idea
how to correct this part.)


Any suggested correction will be appreciated.

Thank you

Answer Source

when you create a new Guess(), it runs the constructor of Dice automatically, since Guess extends dice. The secret number in the Guess instance is not the secret number of the first Dice instance, it's a separate instance. I think your design is flawed. Why does Guess need to extend Dice? A guess is not logically a different implementation of a Dice (which is what you'd (logically) normally use subclasses for).

Here's how I would do it (without knowing anything further about what exactly you want to achieve):

<?php
class Dice
{
  public $maxPossibleNo;
  public $secretNumber;

  function __construct($no_of_dice=1) 
  {
    // how many possible dice number to enroll
    $this->maxPossibleNo = $no_of_dice * 6;

    // do shaking dice
    $this->secretNumber = $this->getSecretNumber();
  }

  function getSecretNumber() 
  {
    return rand(1, $this->maxPossibleNo);
  }

  function roll() 
  {
    $found = false;
    list($array1, $array2) = array_chunk(range(1, $this->maxPossibleNo), ceil($this->maxPossibleNo/2));
    /*
        Array
        (
            [0] => 1
            [1] => 2
            [2] => 3
        )
        Array
        (
            [0] => 4
            [1] => 5
            [2] => 6
        )
    */
  }
}

class Guess
{
  private $dice;

  function __construct(Dice $d)
  {
    $this->dice = $d;
  }

  function checkSecretNumber() 
  {
    return $this->dice->secretNumber;
  }

  function isGreaterThan($x) 
  {
    return $x > $this->dice->secretNumber;
  }

  function isLessThan($x) 
  {
    return $x < $this->dice->secretNumber;
  }

  function isEqual($x) 
  {
    return $x == $this->dice->secretNumber;
  }
}

$dice = new Dice('1');
$dice->roll();
$guess = new Guess($dice);
echo $dice->secretNumber;
echo $guess->checkSecretNumber();