Created
June 27, 2015 07:32
-
-
Save ken-master/7d89a4c99cddd7f2f3ae to your computer and use it in GitHub Desktop.
PHP Strategy Pattern Example 2
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 | |
interface AnimalInterface{ | |
public function run(); | |
} | |
class Dog implements AnimalInterface{ | |
public function run() | |
{ | |
return 'running while barking'; | |
} | |
} | |
class Cat implements AnimalInterface{ | |
public function run() | |
{ | |
return 'running while meowing'; | |
} | |
} | |
class Animal{ | |
protected $animal; | |
public function __construct(AnimalInterface $animal) | |
{ | |
$this->animal = $animal; | |
} | |
public function run() | |
{ | |
return $this->animal->run(); | |
} | |
} | |
$dog = new Animal(new Dog()); | |
echo $dog->run(); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment