Skip to content

Instantly share code, notes, and snippets.

View austinbenerdy's full-sized avatar
🐒
Code Monkey Like Fritos

Austin Adamson austinbenerdy

🐒
Code Monkey Like Fritos
View GitHub Profile
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\GoodExample;
$pickaxe = new Pickaxe();
$hammer = new Hammer();
$player1 = new PlayerCharacter($pickaxe);
$player1->useTool();
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\GoodExample;
class PlayerCharacter
{
private IToolInterface $tool;
public function __construct(IToolInterface $startingTool)
{
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample;
$pickaxe = new Pickaxe();
$hammer = new Hammer();
$player = new PlayerCharacter($pickaxe);
$player->useTool(); // use pickaxe
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample;
class PlayerCharacter
{
private IToolInterface $tool;
public function __construct(Pickaxe $pickaxe)
{
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample;
class Hammer implements IToolInterface
{
private int $weight;
public function use() :void
{
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample;
class Pickaxe implements IToolInterface
{
private int $weight;
public function use() :void
<?php
namespace OrdinaryEngineer\CodeExamples\DependencyInversionPrinciple\BadExample;
interface IToolInterface
{
public function use() :void;
public function getWeight() :int;
}
<?php
namespace OrdinaryEngineer\CodeExamples\InterfaceSegregationPrinciple\GoodExample;
class Bandit implements IWeaponBasedEnemyInterface
{
private int $_health;
private int $_maxHealth = 1000;
private IWeapon $_currentWeapon;
<?php
namespace OrdinaryEngineer\CodeExamples\InterfaceSegregationPrinciple\GoodExample;
class Flyer implements IEnemyInterface
{
private int $_health;
private int $_maxHealth = 1000;
public function __construct()
<?php
namespace OrdinaryEngineer\CodeExamples\InterfaceSegregationPrinciple\GoodExample;
interface IWeaponBasedEnemyInterface extends IEnemyInterface
{
public function getWeaponDamageValue() :int;
public function swapWeapon(IWeapon $newWeapon) :void;
}