Inheritance is one of the most important pillars of object-oriented
programming. It allows us to create a new class that automatically inherits all public and protected
properties and methods of an existing class. In PHP, we achieve this magic using the
extends keyword.
With this concept, we create a natural hierarchy between classes, which in programming is described as a Parent → Child or Superclass → Subclass relationship. Imagine the following real-world example:
$brand and $speed, and a move() method.extends Vehicle, it automatically inherits all
these properties, and in addition, we can add specific things that a general vehicle does not have (e.g., the
$doorsCount property or the openTrunk() method).The main goal of inheritance is to drastically reduce code duplication in your projects (strictly following the DRY principle). Instead of writing identical functions for starting the engine or driving for a Car, a Motorcycle, and a Truck, you write this logic only once in the parent Vehicle class. All other subclasses then simply use it or adapt (override) it as needed.
By using inheritance, your applications become extremely modular, easier to maintain, and ready for quick additions of new, more advanced functionalities without interfering with already working code.