I'm quite new to using OOP and wanted to create a simple cardgame. I got the following code:
class card{
private $suit;
private $rank;
public function __construct($suit, $rank){
$this->suit = $suit;
$this->rank = $rank;
}
public function test(){
echo $this->suit.' '.$this->rank;
}
}
class deck{
private $suits = array('clubs', 'diamonds', 'hearts', 'spades');
private $ranks = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 'J', 'Q', 'K', 'A');
public function create_deck(){
$cards = array();
foreach($this->suits as $suit) {
foreach ($this->ranks as $rank) {
$cards[] = new card($suit, $rank);
}
}
print_r($cards);
}
}
Say, for example that my class card had a function for dealing a card. How do I deal a king of hearts? which is already created but I don't know how to access it.
$card = new card('hearts', 'K');will deal the king of hearts and assign it to the$cardvariable.