Object-Oriented Programming (OOP) in PHP isn't just about classes and objects. Itβs about making your code cleaner, reusable, and modular. Inheritance and Interfaces help you do just that!
π― In this guide, youβll learn:
β
What inheritance is and how it works
β
How interfaces enforce consistency in your code
β
How to use abstract classes
β
A real-world example using OOP principles
Letβs dive in! π
1οΈβ£ What Is Inheritance in PHP?
π‘ Inheritance lets one class reuse properties and methods from another class.
πΉ Parent Class = The original class (Base class).
πΉ Child Class = The class that extends the parent class (Derived class).
Example: Basic Inheritance
<?php
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function makeSound() {
return "$this->name makes a sound.";
}
}
// Dog class inherits from Animal
class Dog extends Animal {
public function makeSound() {
return "$this->name barks.";
}
}
$dog = new Dog("Buddy");
echo $dog->makeSound(); // Output: Buddy barks.
?>
π₯ Whatβs happening?
β
Dog class inherits from Animal (extends Animal
).
β
It overrides the makeSound()
method.
2οΈβ£ Overriding Parent Methods
If a child class needs a different behavior, it can override the parentβs method.
Example: Overriding a Method
<?php
class Car {
public function drive() {
return "The car is moving.";
}
}
class Tesla extends Car {
public function drive() {
return "The Tesla is driving autonomously!";
}
}
$myCar = new Tesla();
echo $myCar->drive(); // Output: The Tesla is driving autonomously!
?>
π₯ Why override methods?
β
Allows customization without modifying the original class.
3οΈβ£ Using parent::
to Call Parent Methods
Even if you override a method, you can still call the parent method.
<?php
class Person {
public function greet() {
return "Hello!";
}
}
class Student extends Person {
public function greet() {
return parent::greet() . " I'm a student!";
}
}
$student = new Student();
echo $student->greet(); // Output: Hello! I'm a student!
?>
π₯ Why use parent::
?
β
Keeps the original behavior but adds extra functionality.
4οΈβ£ Understanding Abstract Classes
π‘ Abstract classes are blueprints for other classes.
πΉ They cannot be instantiated.
πΉ They must be extended by other classes.
πΉ They can define abstract methods (methods that must be implemented by child classes).
Example: Using an Abstract Class
<?php
abstract class Vehicle {
abstract public function move(); // Must be implemented by child classes
}
class Bike extends Vehicle {
public function move() {
return "The bike is pedaling.";
}
}
class Car extends Vehicle {
public function move() {
return "The car is driving.";
}
}
$bike = new Bike();
echo $bike->move(); // Output: The bike is pedaling.
$car = new Car();
echo $car->move(); // Output: The car is driving.
?>
π₯ Why use abstract classes?
β
Enforces a common structure for all child classes.
β
Ensures that all vehicles must have a move()
method.
5οΈβ£ What Are Interfaces?
π‘ Interfaces define a set of methods that a class must implement.
πΉ Unlike abstract classes, interfaces cannot have properties.
πΉ A class can implement multiple interfaces, but it can only extend one class.
Example: Using Interfaces
<?php
interface PaymentGateway {
public function processPayment($amount);
}
class PayPal implements PaymentGateway {
public function processPayment($amount) {
return "Processing $$amount payment through PayPal.";
}
}
class Stripe implements PaymentGateway {
public function processPayment($amount) {
return "Processing $$amount payment through Stripe.";
}
}
$paypal = new PayPal();
echo $paypal->processPayment(100); // Output: Processing $100 payment through PayPal.
$stripe = new Stripe();
echo $stripe->processPayment(200); // Output: Processing $200 payment through Stripe.
?>
π₯ Why use interfaces?
β
Enforces consistency (every payment gateway must have processPayment()
).
β
Supports multiple implementations of the same functionality.
6οΈβ£ Abstract Class vs. Interface: Key Differences
Feature | Abstract Class | Interface |
---|---|---|
Properties | β Can have properties | β Cannot have properties |
Method Implementation | β Can have implemented methods | β Only method signatures |
Multiple Inheritance | β No (one parent class) | β Yes (multiple interfaces) |
Use Case | When you want some shared behavior | When you want only method requirements |
π― Mini Project: Employee Management System
Letβs build an OOP-based Employee System using inheritance and interfaces.
1οΈβ£ Abstract Employee Class
<?php
abstract class Employee {
protected $name;
protected $salary;
public function __construct($name, $salary) {
$this->name = $name;
$this->salary = $salary;
}
abstract public function getRole(); // Must be implemented by child classes
public function getDetails() {
return "$this->name earns $$this->salary.";
}
}
?>
2οΈβ£ Child Classes (Manager & Developer)
<?php
require "Employee.php";
class Manager extends Employee {
public function getRole() {
return "Manager";
}
}
class Developer extends Employee {
public function getRole() {
return "Developer";
}
}
?>
3οΈβ£ Payroll Interface
<?php
interface Payroll {
public function calculateBonus();
}
class SeniorDeveloper extends Developer implements Payroll {
public function calculateBonus() {
return $this->salary * 0.1; // 10% bonus
}
}
?>
4οΈβ£ Testing the System
<?php
require "Manager.php";
require "Developer.php";
require "SeniorDeveloper.php";
$manager = new Manager("Alice", 6000);
$dev = new Developer("Bob", 5000);
$seniorDev = new SeniorDeveloper("Charlie", 7000);
echo $manager->getDetails() . " Role: " . $manager->getRole() . "<br>";
echo $dev->getDetails() . " Role: " . $dev->getRole() . "<br>";
echo $seniorDev->getDetails() . " Role: " . $seniorDev->getRole() . ". Bonus: $" . $seniorDev->calculateBonus();
?>
π₯ Whatβs happening?
β
Managers and Developers inherit Employee properties
β
SeniorDeveloper implements a Payroll system
β
This structure follows real-world job roles
π Final Thoughts
Now you understand inheritance, interfaces, and abstract classes!
β
Use inheritance to avoid code duplication.
β
Use abstract classes to enforce a base structure.
β
Use interfaces when you need multiple implementations.
π Next: PHP Namespaces
Happy coding! ππ