. , , . . , , , . ( , ):
interface Animal {
public function makeSound();
}
class Dog implements Animal {
public function makeSound() {
echo "WOOF!";
}
}
class Cat implements Animal {
public function makeSound() {
echo "MEOW!";
}
}
class NullAnimal implements Animal {
public function makeSound() {
}
}
$animalType = 'donkey';
$animal;
switch($animalType) {
case 'dog' :
$animal = new Dog();
break;
case 'cat' :
$animal = new Cat();
break;
default :
$animal = new NullAnimal();
}
$animal->makeSound();
Null Object Pattern Class, . . , , - .