title: Generate a random number in a php class
body: <p>I am doing an exercise I found online to teach myself <code>php</code>. The program I am making is a "Guess the Number" game where I need to generate a random number to be guessed. I was successful in the exercise when not using a class and using <code>html</code> input boxes for the users input:</p>
  
  <pre><code>&lt;?php
  if(isset($_POST['number'])) {
      $number = $_POST['number'];
  } else {
      $number = rand(1,99);
  }
  
  if($_POST["guess"]) {
      // grab the user input guess
      $guess  = $_POST['guess'];
      $number  = $_POST['number'];
      $output = '';
      static $count = 0; //Initialize count
  
      if ($guess &lt; $number) { 
          $output .= "$guess" . "&lt;br /&gt;" . "Guess Higher";
          $count++;
      } elseif($guess &gt; $number) {       
          $output .= "$guess" . "&lt;br /&gt;" . "Guess Lower";
          $count++;
      } elseif($guess == $number) {  
          $count++;    
          $output .= "$guess" . "&lt;br /&gt;" . "You got it!" . "&lt;br /&gt;" . "You guessed it in " . $count . " trials";
      }
  } ?&gt;
  
  
  &lt;!DOCTYPE html&gt;
  &lt;html&gt;
      &lt;head&gt;
          &lt;title&gt;Guess A Number&lt;/title&gt;
      &lt;/head&gt;
  
      &lt;body&gt;
          &lt;form action="NumberGuess.php" method="post"&gt;
              &lt;label for="guess"&gt;Guess A Number:&lt;/label&gt;&lt;br/ &gt;
              &lt;input type="text" name="guess" /&gt;
              &lt;input name="number" type="hidden" value="&lt;?= $number ?&gt;" /&gt;
              &lt;input name="submit" type="submit" /&gt;
              &lt;br/&gt;
              &lt;?php echo $output ?&gt;
              &lt;br/&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>However, now I want to repeat the exercise using <code>json</code> as input and using classes/objects. I tried using <code>rand()</code> to generate the random number, however it causes an issue and the page won't load:</p>
  
  <pre><code>class NumberGuess {
      protected $guess = 0;
      const randNumber = rand(1,99);
      protected $isNumber = false;
      protected $higher = true;
  
      function __construct($inputGuess) {
          $this-&gt;guess = $inputGuess;
      }
  
      public function getResult() {
          $this-&gt;numberChecker();
  
          if($this-&gt;higher == true) {
              $this-&gt;higher = "higher";
          } elseif($this-&gt;higher == false) {
              $this-&gt;higher = "lower";
          }
  
          if($this-&gt;isNumber == false) {
              return $this-&gt;guess . " is incorrect, " . "try guessing " . $this-&gt;higher;
          } elseif($this-&gt;isNumber == true) {
              return $this-&gt;guess . " is correct!";
          }
      }
  
      private function numberChecker() {
          if($this-&gt;guess &lt; self::randNumber) {
              $this-&gt;higher = true;
              $this-&gt;isNumber = false;
          } elseif($this-&gt;guess &gt; self::randNumber) {
              $this-&gt;higher = false;
              $this-&gt;isNumber = false;
          } elseif($this-&gt;guess == self::randNumber) {
              $this-&gt;higher = false;
              $this-&gt;isNumber = true;
          }
      }
  }
  
  if ( isset($_SERVER['QUERY_STRING']) ) {
      $inputString = $_SERVER['QUERY_STRING'];
      $inputGuess = intval($inputString);
  }
  
  $myGuess = new NumberGuess($inputGuess);
  echo json_encode($myGuess-&gt;getResult());
  </code></pre>
  
  <p>Could someone please explain to me how I can generate a random number in a class using <code>json</code>?</p>