[英]Can i define an object inside a Class constructor?
干净的代码说,不建议初始化对象,在__constructor中使用if语句或其他方式。 我有一个类,其中我在构造函数中使用了不允许的元素。 如何重建它以符合规则?
我在Google上搜索! 但是我并不真正理解,我希望我能通过这个特定的例子成功地理解。 完整的代码也可以在github上找到: https : //github.com/KoreLewi/21-blackjack-game
public function __construct(array $Cards = array())
{
if (empty($Cards)) {
$Cards = $this->initEnglishDeck();
}
parent::__construct($Cards);
}
public function initEnglishDeck()
{
$arrCards = array();
foreach ($this->suits as $suit) {
foreach ($this->cards as $card) {
$arrCards[] = new Card($suit, $card);
}
}
return $arrCards;
}
完整的代码也可以在github上找到: https : //github.com/KoreLewi/21-blackjack-game
模式是依赖注入,而不是内部初始化依赖。
一种“修复”代码的方法是拥有CardDeck
接口和多个(或在这种情况下)实现CardDeck
的EnglishDeck
类。 在所有需要卡片组的类中,您都将其注入到构造函数中,如下所示:
class Game {
/**
* @var CardDeck
*/
private $cardDeck
public function __construct(CardDeck $cardDeck) {
$this->cardDeck = $cardDeck
}
}
在那种情况下,即使您决定让他通过另一种类型的CardDeck,例如FrenchDeck
也将实现CardDeck
接口的FrenchDeck,您的Game类仍然可以工作。
你可以:
将if语句移到initEnglishDeck()函数中。
public function __construct(array $Cards = array()) {
$Cards = $this->initEnglishDeck($Cards);
parent::__construct($Cards);
}
public function initEnglishDeck($cards = array()) {
if(!empty($cards)) return $cards;
$arrCards = array();
foreach ($this->suits as $suit) {
foreach ($this->cards as $card) {
$arrCards[] = new Card($suit, $card);
}
}
return $arrCards;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.