PHP OOP - Inheritance

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
<?php
class Vehicle {
public $brand;
public function __construct($brand) {
$this->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
<?php
class Vehicle {
protected $brand;
public function __construct($brand) {
$this->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
<?php
// Parent class
class Vehicle {
public $brand;
public $speed;
public function __construct($brand, $speed) {
$this->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
<?php
final class Vehicle {
public function drive() {
return "Driving a vehicle...";
}
}
// class Car extends Vehicle { // ERROR: Cannot inherit from final class
// }
$myVehicle = new Vehicle();
echo $myVehicle->drive(); // Output: Driving a vehicle...
?>

Try it yourself

The following example shows how to prevent method overriding:

Example
<?php
class Vehicle {
public function start() {
return "Vehicle is starting...";
}
final public function stop() {
return "Vehicle is stopping..."; // Cannot be overridden
}
}
class Car extends Vehicle {
public function start() { // Allowed (not final)
return "Car engine is starting...";
}
// public function stop() { // ERROR: Cannot override final method
// return "Car is stopping...";
// }
}
$myCar = new Car();
echo $myCar->start(); // Output: Car engine is starting...
echo "<br>";
echo $myCar->stop(); // Output: Vehicle is stopping...
?>

Try it yourself


Whereisstuff is simple learing platform for beginer to advance level to improve there skills in technologies.we will provide all material free of cost.you can write a code in runkit workspace and we provide some extrac features also, you agree to have read and accepted our terms of use, cookie and privacy policy.
© Copyright 2024 www.whereisstuff.com. All rights reserved. Developed by whereisstuff Tech.