codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🐘

PHP

45 / 56 topics
43Object-Oriented Programming in PHP44Classes and Objects45Inheritance in PHP46Polymorphism in PHP47Interfaces in PHP48Traits in PHP
Tutorials/PHP/Inheritance in PHP
🐘PHP

Inheritance in PHP

Updated 2026-05-15
10 min read

Inheritance in PHP

Introduction

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.

Concept

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.

Examples

Basic Inheritance

Let's start with a simple example where we create a Vehicle class and then extend it to create a Car class.

php
1<?php
2class Vehicle {
3 public $brand;
4
5 public function __construct($brand) {
6 $this->brand = $brand;
7 }
8
9 public function displayBrand() {
10 echo "This vehicle is a " . $this->brand . ".
11";
12 }
13}
14
15class Car extends Vehicle {
16 public $model;
17
18 public function __construct($brand, $model) {
19 parent::__construct($brand);
20 $this->model = $model;
21 }
22
23 public function displayModel() {
24 echo "This car is a " . $this->model . ".
25";
26 }
27}
28
29$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.

Overriding Methods

You can override methods in the child class to provide specific behavior for that class.

php
1<?php
2class Vehicle {
3 public $brand;
4
5 public function __construct($brand) {
6 $this->brand = $brand;
7 }
8
9 public function displayBrand() {
10 echo "This vehicle is a " . $this->brand . ".
11";
12 }
13}
14
15class Car extends Vehicle {
16 public $model;
17
18 public function __construct($brand, $model) {
19 parent::__construct($brand);
20 $this->model = $model;
21 }
22
23 public function displayBrand() {
24 echo "This car is a " . $this->brand . ".
25";
26 }
27}
28
29$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.

Access Modifiers

It's important to understand how access modifiers affect inheritance:

  • Public: Properties and methods are accessible from anywhere.
  • Protected: Properties and methods are accessible within the class itself and by inheriting classes.
  • Private: Properties and methods are only accessible within the class itself.
php
1<?php
2class Vehicle {
3 protected $brand;
4
5 public function __construct($brand) {
6 $this->brand = $brand;
7 }
8
9 protected function displayBrand() {
10 echo "This vehicle is a " . $this->brand . ".
11";
12 }
13}
14
15class Car extends Vehicle {
16 public function showBrand() {
17 $this->displayBrand(); // Accessible because it's in the same hierarchy
18 }
19}
20
21$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.

What's Next?

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.


PreviousClasses and ObjectsNext Polymorphism in PHP

Recommended Gear

Classes and ObjectsPolymorphism in PHP