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
- Compile-time (static) — method overloading. Limited in PHP: no true overloading, but variadic arguments exist
- Runtime (dynamic) — method overriding through inheritance or interfaces. Most common in PHP