Object Inheritance



It is a concept used to express that objects of one class can be transferred to another class. It is commonly used in situations where the attributes and methods of a class must be used in more than one class. The object inheritance   is done with the extends clause. With this extension, a class can inherit at most one class. There are other ways of getting more than one class into a class. However, this situation will be focused on another time. Two classes are required for this operation . The province is the lower (child) class. The second is the parent (parent) class. For a good understanding of this topic, we will use the Automobile and Aircraft classes, which are sub-types of vehicles and vehicles. The syntax of this process is as follows.

 

 

 

# Syntax


The extend statement is used to extend a class with a begin class.

class SelfClass extends ParentClass
{
    # Kodlarınız
}

It uses all the objects of the upper class that the class inherits itself as if it were its own.

 

 

# Upper Classes (Parent)


Upper classes are inherited by subclasses by extending statement. Upper classes should be designed to include the top objects of the classes to be inherited. Subclasses should be narrower than upper classes.

It is important that the upper classes include the following features;

They should be more extensive.
Inherited subclasses should have common objects.

Although vehicles such as automobiles, airplanes, and trains each have different characteristics, some behaviors are common. Like using fuel or having a color. Therefore, there is a need for a higher class to accommodate the common features of these classes.

File: Vehicle.php
<?php

class Vehicle
{
    protected $color = 'White';
 
    public function startEngine(Array $connection)
    {
        return $connection;
    }

    public function setColor(String $color)
    {
        $this->color = $color;
    }

    public function getColor() : String
    {
        return $this->color;
    }
}

Some of the common features of all vehicles are fuel use and movement. In this respect, these behaviors are specified in the Vehicle :: top class.

 

 

# Subclasses (Self)


The subclass concept is a concept that expresses the inheritance of an upper class with an extends expression. In this way, libraries that expand their methods and qualifications are called child classes. Because they took qualitative and methodological behaviors belonging to the upper class.

File: Automobile.php
<?php

class Automobile extends Vehicle
{
    public function wayType()
    {
        return 'Highway';
    }
}
File: Aircraft.php
<?php

class Aircraft extends Vehicle
{
    public function wayType()
    {
        return 'Airline';
    }
}

Now that each of the above subclasses has a color and is a general feature of these color tools, we have devised methods for this in the Vehicle :: parent class. In the lower grades, the road type, which is a special method, is specified separately.

File: index.php
<?php 

require 'Vehicle.php';
require 'Automobile.php';
require 'Aircraft.php'

$automobile = new Automobile;

$automobile->setColor('red');

echo $automobile->getColor();

$aircraft = new Aircraft;

$aircraft->setColor('blue');

echo $aircraft->getColor();
red
blue

 

 

# Access to Upper Class Objects


A property or method of the parent class may need to be used within the child class. In such cases, the parent :: statement is used.

File: Automobile.php
<?php

class Automobile extends Vehicle
{
    public function wayType()
    {
        return 'Highway';
    }

    public function getWheelColor()
    {
        parent::setColor('blue');
    
        return parent::getColor();
    }
}

In general we will refer hereafter to such use __construct () constructor using lower grades despite superclass in that the same method in the parent class by using the same method parent :: __construct () 's are needed in cases where lose its function.