PHP. OOP. Classes inheritance

Home » Tutorials » PHP » PHP. OOP. Classes inheritance
In this lesson we’ll continue to learn OOP in PHP. I recommend you to watch last lesson for your understanding of toda topic. Today we’ll discuss classes inheritance. Inheritance in php oop is one of the basic and key concept. Two others are encapsulation and polymorphism. We also will learn it today.
Let’s begin from theory – tha basics concepts of oop in php are encapsulation, inheritance and polymorphism.

Encapsulation is capability object or class to hide its internal implementation. They only give user public methods – interface of class. For example in real life you don’t ask yourself how computer saves your data. Because you have got “ready to use” tools – keyboard, mouse and other, which are interface between you and computer. In oop this interface is classes methods.

Inheritance, I am sure, is clear from the title. Class can inherit properties and methods from another class. For example, we inherited a lot from our parents.

Polymorphism is connected wth inheritance. It is object capability to redefine methods of parent classes. For example, in code below we redefined setName() method.

I would like to notice several important things:
try to follow the rule “one class in one file”, don’t define several classes in one file.
In child class you can call parent constructor, using parent:: keyword.

Last, I recommned you to read <a href=”http://php.net/manual/en/language.oop5.php” target=”_blank”>oop php chapter.</a>. I think, this chapter is described with difficult style, you need write some other articles with php  oop topic.

Code lesson

<?php

/*
** Наследование классов (наследовать можно только один класс)
** Есть негласное требование - "один класс - один файл"
** Переопределение свойств и методов
** Переопределение и порядок вызова конструктора
** Деструкторы - вызываются после окончания работы скрипта, или если на объект больше
**/

class Man {

    public $name;
    public $age = 27;

    public function __construct() {
        echo "Вызов конструктора";
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
        return $this->name;
    }

    public function __destruct() {
        echo "Вызов деструктора";
    }

}

class Kamil extends Man {

    public $age = 25;

    public function __construct() {
        echo "Вызов конструктора в дочернем классе" . "<br>";
        parent::__construct();
    }

    public function setName($name) {
        if($name = "Камиль") {
            $this->name = "Еще один Камиль";
        }
        return $this->name;
    }

}

$kamil = new Kamil();
echo "<br>";
print_r($kamil);
echo "<br>";

$kamil->setName("Камиль");

print_r($kamil);
echo "<br>";

0 Comments

Submit a Comment

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


The reCAPTCHA verification period has expired. Please reload the page.

Pin It on Pinterest

Share This