Single inheritance, a class inherits properties and methods from only one parent class.
<?php
class ParentClass
{
/* Parent class definition */
}
class ChildClass extends ParentClass
{
/* Child class extending ParentClass */
}
?>
Example:
<?php
// Parent class
class Vehicle {
public function start() {
echo "Vehicle started.";
}
}
// Child class inheriting from the parent class
class Car extends Vehicle {
public function accelerate() {
echo "Car is accelerating.";
}
}
// Create an object of the child class
$obj = new Car();
// Accessing both class parent and child method
$obj->start(); // Output: Vehicle started.
$obj->accelerate(); // Output: Car is accelerating.
?>
- The Vehicle class is the parent class.
- The Car class is a child class inheriting from Vehicle.
- The Car class inherits the start() method from the Vehicle class.
- The Car class also has its own method accelerate().
- When we create an object of the Car class, we can access methods from both the parent and child classes.