Skip to content

Instantly share code, notes, and snippets.

@ken-master
Created June 27, 2015 07:32
Show Gist options
  • Save ken-master/7d89a4c99cddd7f2f3ae to your computer and use it in GitHub Desktop.
Save ken-master/7d89a4c99cddd7f2f3ae to your computer and use it in GitHub Desktop.
PHP Strategy Pattern Example 2
<?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