This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\GoodExample; | |
$pickaxe = new Pickaxe(); | |
$hammer = new Hammer(); | |
$player1 = new PlayerCharacter($pickaxe); | |
$player1->useTool(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\GoodExample; | |
class PlayerCharacter | |
{ | |
private IToolInterface $tool; | |
public function __construct(IToolInterface $startingTool) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample; | |
$pickaxe = new Pickaxe(); | |
$hammer = new Hammer(); | |
$player = new PlayerCharacter($pickaxe); | |
$player->useTool(); // use pickaxe |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample; | |
class PlayerCharacter | |
{ | |
private IToolInterface $tool; | |
public function __construct(Pickaxe $pickaxe) | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample; | |
class Hammer implements IToolInterface | |
{ | |
private int $weight; | |
public function use() :void | |
{ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample; | |
class Pickaxe implements IToolInterface | |
{ | |
private int $weight; | |
public function use() :void |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample; | |
interface IToolInterface | |
{ | |
public function use() :void; | |
public function getWeight() :int; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\InterfaceSegregationPrinciple\GoodExample; | |
class Bandit implements IWeaponBasedEnemyInterface | |
{ | |
private int $_health; | |
private int $_maxHealth = 1000; | |
private IWeapon $_currentWeapon; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\InterfaceSegregationPrinciple\GoodExample; | |
class Flyer implements IEnemyInterface | |
{ | |
private int $_health; | |
private int $_maxHealth = 1000; | |
public function __construct() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace OrdinaryEngineer\CodeExamples\InterfaceSegregationPrinciple\GoodExample; | |
interface IWeaponBasedEnemyInterface extends IEnemyInterface | |
{ | |
public function getWeaponDamageValue() :int; | |
public function swapWeapon(IWeapon $newWeapon) :void; | |
} |
NewerOlder