Inheritance allows a class to take the functionality of existing classes and override them if necessary.
To inherit one class from another, we need to use the “extends” operator.
Example:
class Person { public $name; function __construct($name) { $this->name = $name; } function displayInfo() { echo "Name: $this->name"; } } class Employee extends Person {//creating a new Employee class that follows our Person class //create a new class property public $company; //override the constructor of the main class function __construct($name, $company) { $this->name = $name; $this->company = $company; } //override the base class method function displayInfo() { echo "Name: $this->name"; echo "Work in $this->company"; } } $tom = new Employee("Tom"); //we can now use parent class methods $tom -> displayInfo();
Redefining Functionality
Having inherited the functionality from the parent class, the child class can add its own properties and methods or override legacy functionality.
Example:
class Person { public $name; function __construct($name) { $this->name = $name; } function displayInfo() { echo "Name: $this->name"; } } class Employee extends Person { //create a new class property public $company; //override the constructor of the main class function __construct($name, $company) { $this->name = $name; $this->company = $company; } //override the base class method function displayInfo() { echo "Name: $this->name"; echo "Work in $this->company"; } } $tom = new Employee("Tom", "Microsoft");// now pass 2 properties to the constructor $tom -> displayInfo();
Calling parent class functionality
If many properties from the parent class are repeated in the child class, the child will use these properties from the parent class, not repeat them again.
If we need to refer to a parent class method, then we can use the parent keyword, after which
a double colon :: is used followed by the method to be called.
For example, rewrite the previous example:
class Person { public $name; function __construct($name) { $this->name = $name; } function displayInfo() { echo "Name: $this->name"; } } class Employee extends Person { public $company; function __construct($name, $company) { //we call the constructor of the parent class in the subclass parent::__construct($name); $this->company = $company; } function displayInfo() { //we also call the parent class method parent::displayInfo(); echo "Work in $this->company"; } } $tom = new Employee("Victor", "Driver"); $tom -> displayInfo();
instanceof
The instanceof operator allows you to check whether an object belongs to a particular class.
$tom instanceof Employee; // return true or false
If the class is inherited then the $tom variable also represents the class and instanceof will return true.
Prohibition of inheritance
When it is necessary to forbid overriding of methods. To do this, you need to specify methods with the final modifier in the parent class:
class Person { public $name; function __construct($name) { $this->name = $name; } final function displayInfo() { echo "Name: $this->name"; } }
We can prevent inheritance from an entire class. To do this, this class must be defined with the final modifier:
final class Person { public $name; function __construct($name) { $this->name = $name; } final function displayInfo() { echo "Name: $this->name"; } }