我正在开发一个用 PHP 编写的纸牌游戏。我为每张卡片创建了一个类,并为存放卡片的卡片组创建了一个类。这是我的逻辑片段:
class Card{
private $width;
private $height;
private $value;
private $suit;
private $face;
function __contruct($suit, $face, $width,$height, $value)
{
$this->suit = $suit;
$this->face = $face;
$this->width = $width;
$this->height = $height;
$this->value = $value;
}
}
class Deck{
private $deck = array();
private $suits = array ("Spades", "Hearts", "Clubs", "Diamonds");
private $value = array(2,3,4,5,6,7,8,9,10,11,12,13,14);
private $faces = array (
"Two", "Three", "Four", "Five", "Six", "Seven", "Eight",
"Nine", "Ten", "Jack", "Queen", "King", "Ace"
);
public function __construct()
{
$i = 0;
foreach ($this->suits as $suit[])
{
$width -= 73;
foreach($this->faces as $face)
{
$height -= 98;
//['face'] . ' of ' . $card['suit'];
$this->deck[] = new Card($suit, $face, $width, $height, $value);
}
}
public function getDeck()
{
return $this->deck;
}
}
在驱动程序中,我想引用卡片类中的成员。例如:西装、脸型、价值等
这就是我尝试过的 $deck = new Deck(); $cards = var_dump($deck->getDeck()); //我试图引用卡片类的成员 $cards[0]->suit;
为了学习,我正在使用面向对象的方法。
当我使用上面列出的 var_dump 时,我得到空值而不是卡类成员的值。这不是我所期望的;我期望诸如心,钻石等值。