PHP, as an object-oriented language, supports inheritance, enabling developers to build complex applications efficiently. This article explores class inheritance in PHP, focusing on extending classes, overriding methods, and understanding access modifiers.
Extending Classes
What is Class Inheritance?
Class inheritance allows a class (child class) to inherit the properties and methods of another class (parent class). The child class can use and extend the functionality of the parent class, promoting code reuse and logical structuring.
Defining a Parent Class
Let’s start by defining a simple parent class:
name = $name;
}
public function speak() {
return "The animal makes a sound.";
}
}
?>
In this example, the Animal class has a property $name and a method speak(). This class serves as a base for other more specific animal classes.
Extending a Parent Class
To extend a parent class, use the extends keyword. The child class will inherit all public and protected properties and methods from the parent class. Here’s an example of a child class that extends the Animal class:
name; // Outputs: Buddy
echo $dog->speak(); // Outputs: The dog barks.
?>
In this example, the Dog class extends the Animal class. It inherits the name property and the speak() method from the parent class but overrides the speak() method to provide a specific implementation for dogs.
Overriding Methods
What is Method Overriding?
Method overriding allows a child class to provide a specific implementation of a method that is already defined in its parent class. The child class’s method will be called instead of the parent class’s method when the method is invoked on an instance of the child class.
Example of Method Overriding
Continuing from the previous example, let’s define another child class and override its method:
name; // Outputs: Whiskers
echo $cat->speak(); // Outputs: The cat meows.
?>
In this example, the Cat class extends the Animal class and overrides the speak() method to provide a specific implementation for cats.
Calling the Parent Method
In some cases, you might want to call the parent class’s method within the overridden method in the child class. You can achieve this using the parent keyword:
speak(); // Outputs: The animal makes a sound. Specifically, the bird chirps.
?>
In this example, the Bird class overrides the speak() method and calls the parent class’s speak() method using parent::speak().
Access Modifiers
Access modifiers control the visibility of properties and methods in classes. PHP supports three access modifiers: public, protected, and private.
Public
Properties and methods declared as public can be accessed from anywhere, both inside and outside the class.
name = $name;
}
public function getName() {
return $this->name;
}
}
$person = new Person("John Doe");
echo $person->name; // Outputs: John Doe
echo $person->getName(); // Outputs: John Doe
?>
In this example, the name property and the getName() method are public, so they can be accessed from outside the Person class.
Protected
Properties and methods declared as protected can only be accessed within the class itself and by classes derived from it.
salary = $salary;
}
protected function getSalary() {
return $this->salary;
}
}
class Manager extends Employee {
public function showSalary() {
return $this->getSalary();
}
}
$manager = new Manager(50000);
echo $manager->showSalary(); // Outputs: 50000
// The following line will cause an error because $salary is protected
// echo $manager->salary; // Error
?>
In this example, the salary property and the getSalary() method are protected, so they cannot be accessed directly from outside the Employee class or Manager class. The showSalary() method in the Manager class is used to access the protected getSalary() method.
Private
Properties and methods declared as private can only be accessed within the class itself. They are not accessible from derived classes or outside the class.
balance = $balance;
}
private function getBalance() {
return $this->balance;
}
public function showBalance() {
return $this->getBalance();
}
}
$account = new BankAccount(1000);
echo $account->showBalance(); // Outputs: 1000
// The following lines will cause errors because $balance and getBalance() are private
// echo $account->balance; // Error
// echo $account->getBalance(); // Error
?>
In this example, the balance property and the getBalance() method are private, so they cannot be accessed from outside the BankAccount class. The showBalance() method is provided to access the private getBalance() method.
Combining Inheritance with Access Modifiers
Combining inheritance with access modifiers allows for robust class designs that enforce encapsulation and control over property and method visibility.
type = $type;
$this->speed = $speed;
$this->engineNumber = $engineNumber;
}
protected function getSpeed() {
return $this->speed;
}
private function getEngineNumber() {
return $this->engineNumber;
}
public function displayInfo() {
return "Type: {$this->type}, Speed: {$this->speed}";
}
}
class Car extends Vehicle {
public function showSpeed() {
return $this->getSpeed();
}
}
$car = new Car("Sedan", 120, "ENG12345");
echo $car->displayInfo(); // Outputs: Type: Sedan, Speed: 120
echo $car->showSpeed(); // Outputs: 120
// The following lines will cause errors because $speed and getSpeed() are protected, $engineNumber and getEngineNumber() are private
// echo $car->speed; // Error
// echo $car->getSpeed(); // Error
// echo $car->engineNumber; // Error
// echo $car->getEngineNumber(); // Error
?>
In this example, the Vehicle class has public, protected, and private members. The Car class inherits from Vehicle and can access protected members but not private members.
Conclusion
Inheritance in PHP allows for creating hierarchical class structures where child classes inherit and extend the functionality of parent classes. By using inheritance, method overriding, and access modifiers (public, protected, private), developers can design flexible and secure applications. Understanding these concepts and how to apply them effectively is crucial for writing maintainable and scalable object-oriented PHP code.