Preloader

Inheritance in PHP

PHP Tutorials
codevigyaan php

In PHP inheritance refers to the ability of one class to use the properties and methods of another class. Inheritance is used by programmers to reuse code and to structure programs.

Rather than re-writing the same code, one approach is to create a base class that you inherit from. This makes your code easier to understand.

What Is Inheritance in PHP

Inheritance refers to constructing a class by user using an in exist class.

The class that is already present is known as the parent class.
The new class is named class child.

The child class is inherited by the parent class to access it methods and properties.

In PHP the inheritance is so work with the extends keyword.

Simple Inheritance Example

class Person {
    public $name;

    public function setName($name) {
        $this->name = $name;
    }
}

class Student extends Person {
    public function showName() {
        echo $this->name;
    }
}

$student = new Student();
$student->setName("Varsha");
$student->showName();

Explanation

First, the class Person already defines the following a property and a method.
Next, the Student class extends the Person class.
Because of inheritance, Student can use setName() without redefining it.

Why Inheritance Is Useful

Inheritance is also beneficial for the quality of code.

It helps you
Reusing code develops
Decrease repetition
Logically plan classes

Hence, the application is easier to maintain, and we can adapt (scales) very easy.

Method Overriding in PHP Inheritance

From time to time a child class might need a altered version of a method. This can be done by means of method overriding.

class Person {
    public function role() {
        echo "I am a person";
    }
}

class Student extends Person {
    public function role() {
        echo "I am a student";
    }
}

$obj = new Student();
$obj->role();

Here, PHP doesn‘t use the parent class but the child class.

Using the parent Keyword in PHP

As you override a method, you may want to use the parent class’ method anyway. PHP provides us with parent keyword.

class Student extends Person {
    public function role() {
        parent::role();
        echo " and I study";
    }
}

This gives you a method to reuse parent logic, plus new features.

Types of Inheritance in PHP

PHP only supports single inheritance.

This implies
A child class cannot inherit a parent class from more than one class.

However PHP provides an additional behavior in the form of interface and trait.

Common Use Cases of Inheritance

Inheritance can often be found for
User and admin roles
Base controller classes
Shared database logic
Form handling systems

Such patterns simplify the management of large-scale projects.

Common Beginner Mistakes

Beginners often
overuse inheritance
forget access modifiers
try multiple inheritance

Keeping class roles simple avoids these problems.

Best Practices for PHP Inheritance

Use inheritance only where is an obvious valid relation between the classes.
Preserve parent classes openended.
Limit the depth of inheritance chains.
Whenever feasible, prefer composition

These best practices keep the Object Oriented PHP tidy and flexible.

Summary

Inheritance is a feature of Object Oriented PHP which allows a class to utilize other existing PHP code. This is done using parent and child classes and it is a way of avoiding repetition and makes it easier to manage your applications. When used correctly then inheritance adds to the strength of Object Oriented PHP.

Check out our resources!

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *