2012-07-01 16 views
1
class Player 
{ 
    public $name; 
    public $stack; 
    public $action; 

    public function __construct($stack, $action) 
    { 
     $this->action = $action; 
     $this->stack = $stack; 
    } 

} 

class Hand 
{ 
    public $BB; 
    public $SB; 
    public $ante; 
    public $isSB = TRUE; 
    public $hero = 1; 

    public $players = array(
     new Player(1800,200); 
     new Player(2000,0); 
     new Player(2400,100); 
     new Player(2600,200); 
    ); 

} 


$h = new Hand(); 
echo $h->players[$h->hero]->stack; 

我得到了解析錯誤。 解析錯誤:語法錯誤,意外的T_NEW,期待')'在... 如何添加對象到這個數組?有更好的解決方案嗎?PHP - 數組中的新對象

+0

你不能做到這一點。數組中的第一項由逗號分隔。其次:你需要在一個方法中做到這一點。 – PeeHaa

回答

2

你需要將它移動到構造

class Hand 
{ 
    public $BB; 
    public $SB; 
    public $ante; 
    public $isSB = TRUE; 
    public $hero = 1; 

    public $players ; 

    public function __construct() 
    { 
     $this->players = array(
      new Player(1800,200), 
      new Player(2000,0), 
      new Player(2400,100), 
      new Player(2600,200), 
     ); 
    } 

}