PHP Tutorial
PHP Flow Control
PHP Functions
PHP String
PHP Array
PHP Date Time
PHP Object Oriented
Regular Expression
PHP Cookie & Session
PHP Error & Exception handling
MySQL in PHP
PHP File Directory
PHP Image Processing
Inheritance is a key feature of object-oriented programming. It allows you to define a class based on another class. This makes creating and maintaining an application much easier. The class that is inherited from is called the parent or superclass, and the class that inherits from the parent is called the child or subclass.
Here's a basic example of inheritance in PHP:
class Vehicle { public $wheels; public function setWheels($wheels) { $this->wheels = $wheels; } } class Car extends Vehicle { public $doors; public function setDoors($doors) { $this->doors = $doors; } }
In this example, Car
is a subclass of Vehicle
. This means that Car
inherits all public and protected methods and properties of Vehicle
. You can use these just as if they were directly inside Car
.
You can use the Car
class like this:
$myCar = new Car(); $myCar->setWheels(4); $myCar->setDoors(4); echo $myCar->wheels; // Outputs: 4 echo $myCar->doors; // Outputs: 4
In the code above, even though the setWheels()
method is defined in the Vehicle
class, you can use it in an instance of the Car
class. This is because Car
inherits from Vehicle
.
One important thing to note about inheritance is the visibility of properties and methods:
If you have a method in the child class with the same name as a method in the parent class, the child class method will override the parent class method. This is known as method overriding. If you still want to call the parent class method, you can do so with parent::methodName()
.
Creating and extending classes in PHP:
<?php // Base class class Animal { public function makeSound() { echo 'Generic animal sound'; } } // Subclass extending Animal class Dog extends Animal { public function makeSound() { echo 'Bark'; } } // Create instances $animal = new Animal(); $dog = new Dog(); // Output sounds $animal->makeSound(); // Output: Generic animal sound $dog->makeSound(); // Output: Bark
Inheriting properties and methods in PHP:
<?php // Base class class Shape { protected $color; public function setColor($color) { $this->color = $color; } } // Subclass extending Shape class Square extends Shape { public function draw() { echo 'Drawing a square with color ' . $this->color; } } // Create instances $square = new Square(); $square->setColor('red'); // Output drawing $square->draw(); // Output: Drawing a square with color red
Access modifiers (public, private, protected) in PHP inheritance:
<?php // Base class class Car { protected $model; public function setModel($model) { $this->model = $model; } } // Subclass extending Car class SportsCar extends Car { public function displayModel() { echo 'Model: ' . $this->model; } } // Create instances $sportsCar = new SportsCar(); $sportsCar->setModel('Ferrari'); // Output model $sportsCar->displayModel(); // Output: Model: Ferrari
Overriding methods in PHP subclasses:
<?php // Base class class Animal { public function makeSound() { echo 'Generic animal sound'; } } // Subclass extending Animal class Dog extends Animal { public function makeSound() { echo 'Bark'; } } // Create instances $animal = new Animal(); $dog = new Dog(); // Output sounds $animal->makeSound(); // Output: Generic animal sound $dog->makeSound(); // Output: Bark
Abstract classes and methods in PHP inheritance:
<?php // Abstract class abstract class Shape { protected $color; abstract public function draw(); public function setColor($color) { $this->color = $color; } } // Concrete subclass extending Shape class Square extends Shape { public function draw() { echo 'Drawing a square with color ' . $this->color; } } // Create instance $square = new Square(); $square->setColor('blue'); // Output drawing $square->draw(); // Output: Drawing a square with color blue
Final classes and preventing further inheritance in PHP:
final
to prevent further subclassing.<?php // Final class final class Vehicle { // Class definition } // Attempt to extend a final class (will result in an error) // class Car extends Vehicle { // // Class definition // }
Using parent
and self
keywords in PHP inheritance:
parent
and self
keywords for referencing parent and current class.<?php // Base class class Animal { protected $name; public function setName($name) { $this->name = $name; } } // Subclass extending Animal class Dog extends Animal { public function bark() { echo 'Woof!'; } public function setName($name) { // Use parent::setName to access the parent class method parent::setName($name); // Or use self::setName to access the current class method // self::setName($name); } } // Create instance $dog = new Dog(); $dog->setName('Buddy'); // Output name echo $dog->name; // Output: Buddy