Glossary

Polymorphism

Polymorphism is the ability of objects from different classes to respond to the same method call in different ways. Code calling the method through an interface or parent class does not know — and does not need to know — which subclass will execute the logic.

Example

interface Shape {
  area(): float;
}
class Circle implements Shape {
  area(): float { return M_PI * $this->r ** 2; }
}
class Square implements Shape {
  area(): float { return $this->side ** 2; }
}

function printArea(Shape $s): void {
  echo $s->area(); // does not know whether it is a circle or square
}

Two types