Why a Review?
Before we dive deep into advanced OOP techniques, it's crucial to revisit and solidify our understanding of the three main pillars of Object-Oriented Programming. Every advanced concept we'll explore is built upon this fundamental foundation.
1. Encapsulation
The Concept: Encapsulation is about "hiding" the complex implementation details inside an object and exposing only a controlled, public interface. Think of a medication capsule: you swallow it to get the benefits without needing to know the chemical formula inside.
In Practice: We use the visibility keywords public
, protected
, and private
to control access to a class's properties and methods.
public
: Can be accessed from anywhere.protected
: Can only be accessed within the class itself and by its child classes (inheritance).private
: Can only be accessed from within the class that defined it.
Example:
class Car {
// Hide the fuel level from the outside world
private $fuel;
public function __construct() {
$this->fuel = 0;
}
// Provide a public method to add fuel
public function addFuel($amount) {
if ($amount > 0) {
$this->fuel += $amount;
}
}
// Provide a public method to check the fuel level
public function getFuel() {
return $this->fuel . " liters";
}
}
$myCar = new Car();
// $myCar->fuel = -100; // This will cause a fatal error!
$myCar->addFuel(50);
echo $myCar->getFuel(); // Outputs: 50 liters
2. Inheritance
The Concept: Inheritance allows a new class (the child class) to acquire all the properties and methods of an existing class (the parent class). This promotes code reuse and establishes a logical hierarchy between classes. It's like a family, where children inherit traits from their parents.
In Practice: We use the extends
keyword.
Example:
// Parent Class
class Vehicle {
public function honk() {
echo "Beep beep!";
}
}
// Child Class inherits from Vehicle
class Motorcycle extends Vehicle {
public function wheelie() {
echo "Performing a wheelie!";
}
}
$myMotorcycle = new Motorcycle();
$myMotorcycle->honk(); // Can call the parent's method
echo "<br>";
$myMotorcycle->wheelie(); // Calls its own method
3. Polymorphism
The Concept: Polymorphism (from Greek, meaning "many forms") is the ability for different objects to respond to the same method call in their own unique way. Think of a USB port: you can plug in a mouse, a keyboard, or a flash drive, and the computer knows how to interact with each one through the same interface.
In Practice: This is often achieved through inheritance (method overriding) or, more commonly, by implementing an interface
.
Example:
interface Animal {
public function makeSound();
}
class Dog implements Animal {
public function makeSound() {
echo "Woof! Woof!";
}
}
class Cat implements Animal {
public function makeSound() {
echo "Meow!";
}
}
// This function can accept any object that is an "Animal"
function letAnimalSpeak(Animal $animal) {
$animal->makeSound();
}
$myDog = new Dog();
$myCat = new Cat();
letAnimalSpeak($myDog); // Outputs: Woof! Woof!
echo "<br>";
letAnimalSpeak($myCat); // Outputs: Meow!
In this example, both Dog
and Cat
objects can be passed to the letAnimalSpeak
function. The function calls the same makeSound()
method, but the output is different depending on the object's class. This is the power of polymorphism.