Polymorphism is - when multiple classes have different functionality but they all share common interface
interface Element {
public function characteristics();
}
class Water implements Element {
public function characteristics() {
return [
'Water characteristic 1',
'Water characteristic 2',
'Water characteristic 3',
];
}
}
class Fire implements Element {
public function characteristics() {
return [
'Fire characteristic 1',
'Fire characteristic 2',
'Fire characteristic 3',
];
}
}
class Air implements Element {
public function characteristics() {
return [
'Air characteristic 1',
'Air characteristic 2',
'Air characteristic 3',
];
}
}
class Earth implements Element {
public function characteristics() {
return [
'Earth characteristic 1',
'Earth characteristic 2',
'Earth characteristic 3',
];
}
}
function describe(Element $element) {
echo get_class($element) . "\n";
$characteristics = $element->characteristics();
if (is_array($characteristics)) {
foreach ($characteristics as $characteristic) {
echo $characteristic . "\n";
}
echo "\n\n";
}
}
$element = new Water;
describe($element);
$element = new Fire;
describe($element);
$element = new Air;
describe($element);
$element = new Earth;
describe($element);