Welcome back to the Object-Oriented Programming (OOP) adventure in PHP! Today, we're diving into two powerful OOP concepts: Inheritance and Polymorphism. These allow you to write clean, reusable, and scalable code. Let's break it down with real-world examples and, of course, a touch of humor!
What is Inheritance?
Inheritance lets one class inherit properties and methods from another class. This helps avoid redundant code and makes programs easier to maintain.
Imagine you run a Zoo and have a base class Animal
that all animals share.
Example: Defining a Parent and Child Class
class Animal {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function makeSound() {
return "Some generic animal sound!";
}
}
class Dog extends Animal {
public function makeSound() {
return "Woof! Woof!";
}
}
$dog = new Dog("Buddy");
echo $dog->makeSound();
// Output: Woof! Woof!
Key Takeaways:
Dog
class inherits from Animal
. makeSound()
method is overridden in Dog
class
What is Polymorphism?
Polymorphism lets child classes modify or extend the behavior of parent methods. This means different objects can respond differently to the same method.
Example: Different Animals, Same Method
class Cat extends Animal {
public function makeSound() {
return "Meow!";
}
}
class Cow extends Animal {
public function makeSound() {
return "Moo!";
}
}
$cat = new Cat("Whiskers");
$cow = new Cow("Betsy");
echo $cat->makeSound(); // Output: Meow!
echo $cow->makeSound(); // Output: Moo!
Key Takeaways:
Cat
and Cow
classes override makeSound()
. The same method produces different outputs
Real-World Example: Employee Management System
Let's create a system where different types of employees (Full-Time & Freelance) have different salary structures.
class Employee {
protected $name;
public function __construct($name) {
$this->name = $name;
}
public function getSalary() {
return 0; // Base class has no salary
}
}
class FullTimeEmployee extends Employee {
public function getSalary() {
return 5000; // Fixed salary
}
}
class FreelanceEmployee extends Employee {
private $hoursWorked;
private $rate;
public function __construct($name, $hoursWorked, $rate) {
parent::__construct($name);
$this->hoursWorked = $hoursWorked;
$this->rate = $rate;
}
public function getSalary() {
return $this->hoursWorked * $this->rate; // Salary based on hours worked
}
}
$fullTime = new FullTimeEmployee("Alice");
$freelancer = new FreelanceEmployee("Bob", 160, 25);
echo "Alice's Salary: $" . $fullTime->getSalary(); // Output: Alice's Salary: $5000
echo "Bob's Salary: $" . $freelancer->getSalary(); // Output: Bob's Salary: $4000
Key Takeaways:
FullTimeEmployee
and FreelanceEmployee
inherit from Employee
, Each class implements getSalary()
differently
Now you understand Inheritance and Polymorphism in PHP with practical examples! Inheritance allows one class to extend another class, Polymorphism allows different classes to override methods, Real-world use cases include animal behaviors and employee salary systems.
0 Comments