Inheritance and Polymorphism in PHP
- Published on
Introduction
Inheritance and Polymorphism are pivotal concepts in Object-Oriented Programming (OOP), fostering code reusability and flexibility. In PHP, these concepts allow developers to create scalable and maintainable applications by enabling classes to share or override behaviors and attributes.
Inheritance in PHP
What is Inheritance?
Inheritance allows a class (child/subclass) to use methods and properties of another class (parent/superclass). It promotes code reusability as the behaviors defined in the superclass can be leveraged by the subclass.
Utilizing Inheritance
In PHP, the extends
keyword is used to implement inheritance.
class Vehicle {
public function startEngine() {
echo "Engine started";
}
}
class Car extends Vehicle {
// Additional methods and properties
}
$myCar = new Car();
$myCar->startEngine(); // Output: Engine started
Polymorphism in PHP
What is Polymorphism?
Polymorphism allows objects to be treated as instances of their parent class, rather than their actual class. It provides the ability to use objects of different types through a uniform interface.
Implementing Polymorphism
In PHP, polymorphism is usually achieved through interfaces or abstract classes.
interface Shape {
public function getArea();
}
class Circle implements Shape {
protected $radius;
public function __construct($radius) {
$this->radius = $radius;
}
public function getArea() {
return 3.14 * $this->radius * $this->radius;
}
}
class Square implements Shape {
protected $side;
public function __construct($side) {
$this->side = $side;
}
public function getArea() {
return $this->side * $this->side;
}
}
function calculateArea(Shape $shape) {
return $shape->getArea();
}
$circle = new Circle(5);
$square = new Square(4);
echo calculateArea($circle); // Output: 78.5
echo calculateArea($square); // Output: 16
Conclusion
Inheritance and Polymorphism in PHP enable developers to write cleaner, more reusable, and maintainable code by leveraging the principles of OOP. These concepts pave the way for building robust applications, allowing for a structured approach to code organization and usage, which is particularly beneficial as projects scale.