<?php
class Worker extends Player {
}
class Settler extends Player {
}
class Warrior extends Player {
}
<?php
class Player {
public function greet() {
// say hello
}
public function move() {
// walk
}
public function attack() {
// punch
}
}
<?php
class Worker extends Player {
}
class Settler extends Player {
}
class Warrior extends Player {
public function attack()
{
// attack with axe
}
}
<?php
interface MoveBehaviour {
public function move();
}
class Walk implements MoveBehaviour {
public function move()
{
// walk
}
}
class Run implements MoveBehaviour {
public function move()
{
// run
}
}
class RideHorse implements MoveBehaviour {
public function move()
{
// ride a horse
}
}
<?php
interface AttackBehaviour {
public function attack();
}
class Punch implements AttackBehaviour {
public function attack()
{
// punch
}
}
class AxeAttack implements AttackBehaviour {
public function attack()
{
// attack with axe
}
}
class EyePoke implements AttackBehaviour {
public function attack()
{
// poke in eye
}
}
<?php
class Player {
private MoveBehaviourInterface $moveBehaviour;
private AttackBehaviourInterface $attackBehaviour;
// ...
public function move() {
$this->moveBehaviour->move();
}
public function attack() {
$this->attackBehaviour->attack();
}
}
<?php
class Player {
private MoveBehaviour $moveBehaviour;
private AttackBehaviour $attackBehaviour;
public function __construct(MoveBehaviour $move, AttackBehaviour $attack) {
$this->moveBehaviour = $moveBehaviour;
$this->attackBehaviour = $attackBehaviour;
}
// ...
}
<?php
$player = new Worker(new Walk(), new Punch());
<?php
class Player{
// ...
public function setAttackBehaviour(AttackBehaviour $attackBehaviour){
$this->attackBehaviour = $attackBehaviour;
}
// ...
}
<?php
// ...
$explorer = new Explorer(new Run(),new EyePoke());
// explorer found a sword
$explorer->setAttackBehaviour(new SwordAttack());