[英]Can i define an object inside a Class constructor?
The clean code says, that is not recommended to initialize objects, use if statement or other things in the __constructor. 干净的代码说,不建议初始化对象,在__constructor中使用if语句或其他方式。 I have a class in which I used elements in the constructor which are not allowed.
我有一个类,其中我在构造函数中使用了不允许的元素。 How to rebuild it to conform to the rules?
如何重建它以符合规则?
I searched on google! 我在Google上搜索! But I do not really understand and I hope that I will succeed in understanding with this particular example.
但是我并不真正理解,我希望我能通过这个特定的例子成功地理解。 The full code is also available on github: https://github.com/KoreLewi/21-blackjack-game
完整的代码也可以在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;
}
The full code is also available on github: https://github.com/KoreLewi/21-blackjack-game 完整的代码也可以在github上找到: https : //github.com/KoreLewi/21-blackjack-game
The pattern is Dependency Injection rather than initialising your dependencies internaly. 模式是依赖注入,而不是内部初始化依赖。
One way to "fix" your code is to have a CardDeck
Interface and multiple (or just in this case) a EnglishDeck
class which implements CardDeck
. 一种“修复”代码的方法是拥有
CardDeck
接口和多个(或在这种情况下)实现CardDeck
的EnglishDeck
类。 And in all the classes which require a card deck you inject it in the constructor like this: 在所有需要卡片组的类中,您都将其注入到构造函数中,如下所示:
class Game {
/**
* @var CardDeck
*/
private $cardDeck
public function __construct(CardDeck $cardDeck) {
$this->cardDeck = $cardDeck
}
}
In that case your Game class would still work even if you decide it to pass him another type of CardDeck, eg FrenchDeck
which would also implement the CardDeck
interface. 在那种情况下,即使您决定让他通过另一种类型的CardDeck,例如
FrenchDeck
也将实现CardDeck
接口的FrenchDeck,您的Game类仍然可以工作。
You could: 你可以:
Move the if statement into the initEnglishDeck() function. 将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.