Object-Oriented Programming (OOP) is a programming paradigm that uses "objects" – data structures consisting of data fields and methods together with their interactions – to design applications and computer programs. One of the core concepts in OOP is inheritance, which allows a class to inherit properties and methods from another class.
Inheritance promotes code reusability, as it enables you to create new classes based on existing ones without having to rewrite the same code. This tutorial will guide you through the basics of inheritance in PHP, including how to extend classes and inherit properties and methods.
In PHP, a class can inherit from another class using the extends keyword. The class that inherits is called the child class or subclass, while the class being inherited from is called the parent class or superclass.
When a child class extends a parent class, it automatically gains all the public and protected properties and methods of the parent class. Additionally, the child class can have its own properties and methods, as well as override existing ones from the parent class.
Let's start with a simple example where we create a Vehicle class and then extend it to create a Car class.
1<?php2class Vehicle {3public $brand;45public function __construct($brand) {6$this->brand = $brand;7}89public function displayBrand() {10echo "This vehicle is a " . $this->brand . ".11";12}13}1415class Car extends Vehicle {16public $model;1718public function __construct($brand, $model) {19parent::__construct($brand);20$this->model = $model;21}2223public function displayModel() {24echo "This car is a " . $this->model . ".25";26}27}2829$myCar = new Car("Toyota", "Corolla");30$myCar->displayBrand(); // Output: This vehicle is a Toyota.31$myCar->displayModel(); // Output: This car is a Corolla.32?>
In this example, the Car class extends the Vehicle class. The Car class has an additional property $model and a method displayModel(). It also calls the parent constructor using parent::__construct($brand) to initialize the inherited property.
You can override methods in the child class to provide specific behavior for that class.
1<?php2class Vehicle {3public $brand;45public function __construct($brand) {6$this->brand = $brand;7}89public function displayBrand() {10echo "This vehicle is a " . $this->brand . ".11";12}13}1415class Car extends Vehicle {16public $model;1718public function __construct($brand, $model) {19parent::__construct($brand);20$this->model = $model;21}2223public function displayBrand() {24echo "This car is a " . $this->brand . ".25";26}27}2829$myCar = new Car("Toyota", "Corolla");30$myCar->displayBrand(); // Output: This car is a Toyota.31?>
In this modified example, the displayBrand() method in the Car class overrides the one in the Vehicle class. When you call displayBrand() on an instance of Car, it uses the overridden method.
It's important to understand how access modifiers affect inheritance:
1<?php2class Vehicle {3protected $brand;45public function __construct($brand) {6$this->brand = $brand;7}89protected function displayBrand() {10echo "This vehicle is a " . $this->brand . ".11";12}13}1415class Car extends Vehicle {16public function showBrand() {17$this->displayBrand(); // Accessible because it's in the same hierarchy18}19}2021$myCar = new Car("Toyota");22$myCar->showBrand(); // Output: This vehicle is a Toyota.23?>
In this example, the brand property and displayBrand() method are protected. The Car class can access them because it extends the Vehicle class.
Now that you have a good understanding of inheritance in PHP, the next step is to explore polymorphism. Polymorphism allows objects to be treated as instances of their parent class, enabling more flexible and reusable code.
Stay tuned for the next tutorial where we will dive into polymorphism and how it can enhance your object-oriented programming skills in PHP.