PHP OOP v2 Inheritance

PHP OOP and Inheritance
PHP OOP and Inheritance

What is inheritance in OOP?

In Php OOP (object-oriented programming), inheritance is a well-structured programming principle. This principle regulates the relationship between objects and classes.

With the inheritance property, a class can inherit the properties and methods of another class or extend them.

Note: Extends keyword is used to inherit from a class in PHP.

If you do not know the self, static, parent, const keywords I explained in the 1st article of our series, please read our previous article Php OOP Basics.

Advertisements

Why do we use inheritance in PHP OOP?

  • If we use oop when coding with PHP, we get rid of the code crowd and prevent code repetition.
  • The code we write is more readable by other developers and they can develop with our classes.
  • When we want to create a new class, we save time by inheriting from the main class we have defined once, without the need to define every feature again.

Let’s give an example by defining the person class

class person {
    // We prevented the variables from being accessible outside the class.
    private $name;
    private $surname;
    private $personalNumber;

    public function __construct($Name, $Surname, $PersonalNumber)
    {
        $this->name = $Name;
        $this->surname = $Surname;
        $this->personalNumber = $PersonalNumber;
    }

    // Only classes that inherit from this class can access.
    protected function GetNameSuranme(){
        return $this->name 
        . " " . $this->surname;
    }

    // Only those who inherit from this class have access to the personal id.
    protected function GetPersonalNumber(){
        return " Personal Number: " 
        . $this->personalNumber;
    }

    // Only those who inherit from this class have set authorization.
    protected function SetNameSurname($name, $surname){
        $this->name = $name;
        $this->surname = $surname;
    }

    // Anyone can access it.
    public function printInfo(){
        return "Name & Surname: $this->name $this->surname";
    }
}

$person = new person("John", "Doe", 11111);

$person->printInfo();

/*
Output:
Name & Surname: John Doe
*/

Since the student is also a person, let’s inherit from the person class we previously created to create the student class.

So let’s create a student class and extend from the person class.

Note: With the parent keyword, we access the properties and methods of the inherited class.
// Student Class
class Student extends person {
    // We can add any additional features we want.
    private $studentNumber;

    public function __construct($StudentName, $StudentSurname, $StudentPersonalNumber, $StudentNumber){
        $this->studentNumber = $StudentNumber;
        // Let's call the Construct method from the person class.
        parent::__construct($StudentName, $StudentSurname, $StudentPersonalNumber);
    }

    // Let's expand by adding new methods
    public function GetStudentNumber(){
        return $this->studentNumber;
    }
    
    public function SetStudentNumber($StudentNumber){
        $this->studentNumber = $StudentNumber;
    }

    // Let's write a new method using the method of the class we inherit.
    public function PrintStudentInfo(){
        echo "Student No: $this->studentNumber <br>"
         . parent::printInfo() . "<br>" 
         // We can access the below protected method as we inherit it.
         . parent::GetPersonalNumber();
    }
}

$student = new Student("John", "Doe", 42113, 325);

$student->PrintStudentInfo();

/*
 Output

 Student No: 325
 Name & Surname: John Doe
 Personal Number: 42113
*/
For more information.
You can follow php.net for updates and more about PHP OOP.
https://www.php.net/manual/en/language.oop5.php
You can read my PHP OOP Final Keyword article here.
https://duzzii.com/php-oop-v3-final-keyword/

Please report the parts you see incorrect or incomplete. And don’t forget to subscribe to our newsletter for more articles. Healthy days!

Liked it? Take a second to support Selim on Patreon!
Advertisements

Selim

Backend Developer, Öğrenci, Blogger

You may also like...

Yorum yap

%d blogcu bunu beğendi: