尝试在PHP卡游戏的数组中嵌套对象,以便稍后引用成员

时间:2013-07-10 19:08:05

标签: php

我正在研究用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());     //我试图引用卡片类的成员     $卡[0] - >西装;

为了学习,我正在使用面向对象的方法。

当我使用上面列出的var_dump时,我得到空值而不是卡类中成员的值。这不是我的预期;我期待心,钻石等价值观。

0 个答案:

没有答案