I did all sorts of debugging and referred to a multitude of sources and can't figure this out on my own. I just started working with OOP in PHP and I'm trying to write a poker hand dealer. I attempt to use a recursive function and a loop and and an if else algorithm to avoid creating duplicates in the pokerHand array. My idea of how this code should work is that when a new pokerHand object is created the __construct function will be automatically called (that's the only reason i know of to use a constructor, but i think there's more to it). Then within a do while loop the code initializes a variable $rand which is a rand number between 1 & 52 and in_array checks to see if that "card" has already been entered in the array. If the card has been dealt the __construct function is supposed to call itself to produce another card until a novel card has been dealt before storing it in the array.
I am just starting out as a coder and any feedback on my code here will be of immeasurable value to me and I thank you in advance.
<?php
class pokerHand {
private $_pokerHand = array();
private $_counter = 0;
public function __construct (){
do{
if (in_array ($rand = rand(1,52), $this->_pokerHand) ){
return public function __construct();
} else {
$this->_pokerHand[] = $rand;
$this->counter++;
}
} while ($this->_counter < 5)
public function showHand(){
print_r ($this->_pokerHand);
}
}
$obj = new pokerHand();
$obj => showHand();
?>