Inheritance in PHP. Code examples open

Inheritance in PHP. Code examples

Approved. Code works!
This is exactly the working code that is verified by the moderator or site administrators

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();
! parent:: __construct($name) is essentially the same as Person::__construct

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";
    }
}
Namespace in PHP. Code example open
Namespace in PHP. Code example
May 16, 2022
Roman-tk
0

More

Leave a Reply

Your email address will not be published. Required fields are marked *

How many?: 22 + 22

lil-code© | 2022 - 2024
Go Top
Authorization
*
*
Registration
*
*
*
*
Password generation