Inheritance in PHP
? Inheritance in PHP
Inheritance is a fundamental concept in Object-Oriented Programming (OOP) that allows one class to inherit the properties and methods of another class. In PHP, inheritance allows you to create a new class that is based on an existing class, which helps in code reuse and creating a hierarchical relationship between classes.
Key Concepts:
Parent Class: The class whose properties and methods are inherited by another class.
Child Class: The class that inherits the properties and methods of the parent class.
extendsKeyword: The keyword used to define inheritance in PHP.
1. Basic Inheritance in PHP
In PHP, a child class can inherit methods and properties from a parent class using the extends keyword.
Syntax:
class ParentClass { // Parent class code}class ChildClass extends ParentClass { // Child class code}Example: Basic Inheritance
<?php// Parent classclass Animal { public $name; public function __construct($name) { $this->name = $name; } public function speak() { echo "I am an animal."; }}// Child class inheriting from Animalclass Dog extends Animal { public function speak() { echo "Woof! My name is " . $this->name; }}$dog = new Dog("Rex");$dog->speak(); // Output: Woof! My name is Rex?>In this example:
The
Dogclass inherits thenameproperty and thespeakmethod from theAnimalclass.The
speakmethod in theDogclass overrides thespeakmethod in theAnimalclass.
2. Constructor Inheritance
If the parent class has a constructor, the child class can inherit and call the parent’s constructor using parent::__construct().
Example: Constructor Inheritance
<?php// Parent classclass Animal { public $name; public function __construct($name) { $this->name = $name; } public function speak() { echo "I am an animal named " . $this->name; }}// Child class inheriting from Animalclass Dog extends Animal { public function __construct($name, $breed) { // Calling the parent constructor parent::__construct($name); $this->breed = $breed; } public function speak() { echo "Woof! I am a " . $this->breed . " dog named " . $this->name; }}$dog = new Dog("Rex", "Golden Retriever");$dog->speak(); // Output: Woof! I am a Golden Retriever dog named Rex?>In this example, the child class Dog calls the parent class Animal constructor using parent::__construct($name) to set the name property.
3. Overriding Methods
In PHP, the child class can override methods from the parent class. When a method in the child class has the same name as one in the parent class, the child class method is used.
Example: Overriding Methods
<?php// Parent classclass Animal { public function speak() { echo "I am an animal."; }}// Child class overriding the speak methodclass Dog extends Animal { public function speak() { echo "Woof! I am a dog."; }}$dog = new Dog();$dog->speak(); // Output: Woof! I am a dog.?>In this example, the Dog class overrides the speak method from the Animal class, so the method in Dog is called when speak is invoked.
4. Accessing Parent Class Methods
To access the parent class's methods or properties from the child class, you can use the parent:: keyword.
Example: Accessing Parent Methods
<?php// Parent classclass Animal { public function speak() { echo "I am an animal."; }}// Child classclass Dog extends Animal { public function speak() { parent::speak(); // Calling the parent class's speak method echo " Woof! I am a dog."; }}$dog = new Dog();$dog->speak(); // Output: I am an animal. Woof! I am a dog.?>In this example, the Dog class calls the speak method from the parent Animal class using parent::speak() and adds its own behavior.
5. Final Keyword in PHP
You can prevent methods in the parent class from being overridden in the child class by marking the method as final. A final method cannot be overridden by child classes.
Example: Final Method
<?php// Parent classclass Animal { final public function speak() { echo "I am an animal."; }}// Child classclass Dog extends Animal { // This will result in an error because speak() is final in the parent class public function speak() { echo "Woof! I am a dog."; }}?>In this example, trying to override the speak method in the Dog class will result in a fatal error because the method is marked as final in the parent class.
6. Abstract Classes and Inheritance
In PHP, an abstract class cannot be instantiated directly. It is meant to be inherited by other classes, which must implement all of its abstract methods.
Abstract Method: A method that is declared in an abstract class but does not have a body. It must be implemented by any child class.
Example: Abstract Class and Inheritance
<?php// Abstract parent classabstract class Animal { public $name; abstract public function speak(); // Abstract method public function setName($name) { $this->name = $name; }}// Child class must implement the abstract methodclass Dog extends Animal { public function speak() { echo "Woof! I am " . $this->name; }}$dog = new Dog();$dog->setName("Rex");$dog->speak(); // Output: Woof! I am Rex?>In this example:
The
Animalclass is abstract and cannot be instantiated directly.The
Dogclass inherits fromAnimaland implements thespeakmethod.
Summary of Inheritance in PHP:
Parent Class: The class from which properties and methods are inherited.
Child Class: The class that inherits from the parent class using the
extendskeyword.Method Overriding: The child class can override methods from the parent class.
Access Parent Methods: Use
parent::to access parent class methods.Abstract Classes: Define abstract classes and methods that must be implemented by child classes.
Final Methods: Prevent methods from being overridden in child classes using the
finalkeyword.