Introduction
Inheritance is one of the most important concepts in Object-Oriented Programming (OOP) in PHP. It allows one class to inherit the properties and methods of another class. This helps developers reuse code, reduce duplication, and create organized applications.
Inheritance makes it easier to manage large projects by creating relationships between parent and child classes.
Objectives
By the end of this training, you will be able to:
- Understand the concept of inheritance in PHP
- Create parent and child classes
- Reuse methods and properties using inheritance
- Override methods in child classes
- Use the
parentkeyword - Understand the benefits of inheritance in web development
What is Inheritance
Inheritance allows a child class to access the properties and methods of a parent class.
The parent class is also called:
- Base class
- Superclass
The child class is also called:
- Derived class
- Subclass
Why Use Inheritance
Inheritance is used to:
- Reuse existing code
- Reduce code duplication
- Improve code organization
- Create scalable applications
- Simplify maintenance
Basic Syntax of Inheritance
<?php
class ParentClass {
// properties and methods
}
class ChildClass extends ParentClass {
// additional properties and methods
}
?>
Example of Inheritance
<?php
class Animal {
public $name = "Cat";
public function sound() {
echo "Animal makes a sound";
}
}
class Cat extends Animal {
public function display() {
echo $this->name;
}
}
$obj = new Cat();
$obj->display();
$obj->sound();
?>
Output
Cat
Animal makes a sound
Understanding Parent Class
A parent class contains common properties and methods that can be shared with child classes.
Example:
<?php
class Vehicle {
public function startEngine() {
echo "Engine Started";
}
}
?>
Understanding Child Class
A child class inherits all accessible methods and properties from the parent class.
Example:
<?php
class Car extends Vehicle {
public function drive() {
echo "Car is moving";
}
}
?>
Accessing Parent Class Methods
<?php
class Person {
public function message() {
echo "Welcome";
}
}
class Student extends Person {
}
$obj = new Student();
$obj->message();
?>
Method Overriding
Method overriding happens when a child class defines a method with the same name as the parent class.
Example:
<?php
class Bird {
public function sound() {
echo "Bird Sound";
}
}
class Parrot extends Bird {
public function sound() {
echo "Parrot Talking";
}
}
$obj = new Parrot();
$obj->sound();
?>
Using the Parent Keyword
The parent keyword is used to access methods or constructors of the parent class.
Example:
<?php
class Employee {
public function company() {
echo "ABC Company";
}
}
class Manager extends Employee {
public function company() {
parent::company();
echo " Manager Department";
}
}
$obj = new Manager();
$obj->company();
?>
Constructor Inheritance
Child classes can inherit constructors from parent classes.
Example:
<?php
class User {
public function __construct() {
echo "User Constructor";
}
}
class Admin extends User {
}
$obj = new Admin();
?>
Types of Inheritance in PHP
Single Inheritance
One child class inherits from one parent class.
Multilevel Inheritance
A class inherits from another child class.
Example:
<?php
class A {
public function test1() {
echo "Class A";
}
}
class B extends A {
}
class C extends B {
}
$obj = new C();
$obj->test1();
?>
Benefits of Inheritance
- Faster development
- Better code reusability
- Easy maintenance
- Improved readability
- Organized project structure
- Supports code scalability
Real World Example of Inheritance
Inheritance is commonly used in:
- School management systems
- E-commerce websites
- Banking applications
- Hospital management systems
- Employee management systems
Example:
- Parent Class: User
- Child Classes: Admin, Customer, Teacher, Student
Best Practices
- Keep parent classes general
- Use inheritance only when classes are related
- Avoid unnecessary inheritance
- Use meaningful class names
- Follow proper coding standards
Common Errors in Inheritance
Incorrect Class Names
class Car extends vehicle
Class names are case-sensitive in some environments.
Accessing Private Members
Private properties and methods cannot be accessed directly in child classes.
Conclusion
Inheritance is a powerful feature in PHP that promotes code reuse and better application structure. It helps developers build scalable and maintainable web applications using Object-Oriented Programming principles.