Advertisement

Google Ad Slot: content-top

PHP OOP - Inheritance

~1 min read · PHP
On this page

PHP - What is Inheritance?


Inheritance in OOP = When a class derives from another class.


Inheritance in Object-Oriented Programming (OOP) allows a child class to acquire the properties and methods of a parent class. In addition, it can have its own properties and methods.


An inherited class is defined by using the extends keyword.


Let's look at an example:

Example
brand = $brand; } } class Car extends Vehicle { public function getBrand() { return "Car Brand: " . $this->brand; } } $myCar = new Car("Toyota"); echo $myCar->getBrand(); // Output: Car Brand: Toyota ?>
Try it yourself

Explanation:


  • The Car class is inherited from the Vehicle class.
  • This means that the Car class can use the public $brand properties as well as the public __construct() methods from the Vehicle class because of inheritance.
  • The Car class also has its own method: getBrand().

PHP - Inheritance and the Protected Access Modifier


Example
brand = $brand; } } class Car extends Vehicle { public function getBrand() { return "Car Brand: " . $this->brand; } } $myCar = new Car("Toyota"); echo $myCar->getBrand(); // Output: Car Brand: Toyota ?>
Try it yourself

Explanation:

  • $brand is protected, so it cannot be accessed directly outside the class.
  • But it can be accessed inside a child class (Car) through getBrand().

PHP - Overriding Inherited Methods


nherited methods can be overridden by redefining the methods (use the same name) in the child class.


Let's create a Vehicle class with a describe() method and override it in a Car subclass.


Example
brand = $brand; $this->speed = $speed; } // Method to describe the vehicle public function describe() { return "This is a vehicle from {$this->brand} with a top speed of {$this->speed} km/h."; } } // Child class (inherits Vehicle) class Car extends Vehicle { public $fuelType; public function __construct($brand, $speed, $fuelType) { $this->brand = $brand; $this->speed = $speed; $this->fuelType = $fuelType; } // Overriding the describe method public function describe() { return "This is a vehicle from {$this->brand} with a top speed of {$this->speed} km/h. It runs on {$this->fuelType}."; } } // Create an instance of Car $myCar = new Car("Toyota", 200, "Petrol"); // Call the overridden method echo $myCar->describe(); ?>
Try it yourself

Explanation


       1. Base Class (Vehicle)

  • Defines brand and speed properties.
  • Has a describe() method to display vehicle details.

       2. Child Class (Car)

  • Inherits Vehicle.
  • Adds a new property $fuelType.
  • Uses parent::describe() to call the parent method and then extends it.

       3. Overriding

  • Car redefines describe(), adding fuel type details.

PHP - The final Keyword


The final keyword in PHP is used to prevent method overriding in child classes or stop a class from being inherited.


The following example shows how to prevent class inheritance:

Example
drive(); // Output: Driving a vehicle... ?>
Try it yourself

The following example shows how to prevent method overriding:

Example
start(); // Output: Car engine is starting... echo "
"; echo $myCar->stop(); // Output: Vehicle is stopping... ?>
Try it yourself