05-06-2015, 07:41 PM
Je me suis renseigné concernant les interfaces composé etc.
En temps normal on ferait un type hint comme ça : Women|Beggar
PHP ne supporte pas encore ça mais visiblement ça va venir un peu plus tard.
Pour le premier cas Xenos
En temps normal on ferait un type hint comme ça : Women|Beggar
PHP ne supporte pas encore ça mais visiblement ça va venir un peu plus tard.
Pour le premier cas Xenos
interface GiveGoldByHero
{
public function giveGoldByHero(Gold $gold, Hero $hero);
}
interface Kid extends GiveGoldByHero { }
interface Women extends GiveGoldByHero { }
interface WomenKid extends Women, Kid { }
interface Hero
{
public function askGoldByWomen(Women $women);
public function askGoldByKid(Kid $kid);
public function askGoldByWomenKid(WomenKid $womenKid);
}
final class Gold
{
private $value;
public function __construct($value)
{
if (is_integer($value) === false) {
throw new RuntimeException('Gold value should be construct from an integer');
}
if ($value < 0) {
throw new RuntimeException('Gold value should be greater than or equal to 0');
}
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
class Conan implements Hero
{
private $gold;
public function __construct(Gold $gold)
{
$this->gold = $gold->getValue();
}
public function askGoldByWomen(Women $women)
{
$this->gold = $this->gold - 2;
$women->giveGoldByHero(new Gold(2), $this);
}
public function askGoldByKid(Kid $kid)
{
$this->gold = $this->gold - 1;
$kid->giveGoldByHero(new Gold(1), $this);
}
public function askGoldByWomenKid(WomenKid $womenKid)
{
$gold = $this->gold;
$this->gold = 0;
$womenKid->giveGoldByHero(new Gold($gold), $this);
}
}
class Gamin implements Kid
{
public function giveGoldByHero(Gold $gold, Hero $hero)
{
echo "Goood i can buy candies for " . $gold->getValue() . " gold\n";
}
}
class BBardot implements Women
{
public function giveGoldByHero(Gold $gold, Hero $hero)
{
echo "I can feed pet with food cost " . $gold->getValue() . " gold\n";
}
}
class Gamine implements WomenKid
{
public function giveGoldByHero(Gold $gold, Hero $hero)
{
echo "I can trick for " . $gold->getValue() . " gold\n";
}
}
$conan = new Conan(new Gold(300));
$gamin = new Gamin;
$bbardot = new Bbardot;
$gamine = new Gamine;
$conan->askGoldByKid($gamin);
$conan->askGoldByWomen($bbardot);
$conan->askGoldByWomenKid($gamine);