Search⌘ K

Limitations of Type Hinting for Objects

Explore the limitations of object type hinting in PHP through a practical example involving BMW and Mercedes classes. Understand how strict type hinting restricts function use to specific classes and why this approach poses challenges when extending functionality to different object types. This lesson helps you grasp important considerations when designing flexible, polymorphic methods in PHP.

We'll cover the following...

Limitations of object type hinting

Let’s consider a scenario. A manager at a car rental company that rents only BMWs hires a programmer to write a program that calculates the price for a full tank of gas for each and every car that the company owns. In accordance with these demands, the programmer writes a class Bmw that holds the code for this task. This class has the relevant data about the BMWs, including the license plate number, car model, and most importantly, a ...