Broad Network


Inheritance in PHP Classes

Object Oriented Programming in PHP – Part 3

Forward: Inheritance is the ability to define new classes using existing classes as a basis. I explain that in this part of the series.

By: Chrysanthus Date Published: 9 Aug 2012

Introduction

This is part 3 of my series, Object Oriented Programming in PHP. Inheritance is the ability to define new classes using existing classes as a basis. I explain that in this part of the series.

Note: If you cannot see the code or if you think anything is missing (broken link, image absent), just contact me at forchatrans@yahoo.com. That is, contact me for the slightest problem you have about what you are reading.

New Properties and Methods
A property or a method is called a member of a class. You can have a class with its members; then you want a new class that will have those same members and new members. Are you going to describe (created) a new class retyping the same old members of the existing class plus new members? PHP exists in such a way that you can have a class with its members and then a new related class with the same members and new members. So, if you want a class that simply has extra members in addition to what an existing class has, you inherit (see below) it from the existing class adding the new members.

Example
The following program shows a class with two properties and one method. Well, it also has the constructor function. The method adds the values of the two properties:

<?php

    class Calculator
        {
            public $num1;
            public $num2;

         function __construct($v1, $v2)
                {
                    $this->num1 = $v1;
                    $this->num2 = $v2;
                }
    
            public function add()
                {
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

        $myObj = new Calculator(6,7);
        $result = $myObj->add();
        echo $result;

?>

Imagine that you want a class that would square a sum (a sum is the addition of two values) and add a fixed value (say 5) to the square. We already have a class that does summing of two values. So, we can derive a class from this existing class. The derived class is the inherited class. It will have an additional property, which will hold the fixed value (5). It will have an additional method that will square the sum and add the fixed value. It inherits the two properties and the add() method of the existing class. The syntax to derive a class from another is:

    class derivedClassName extends baseClassName
        {
            //new members
        }

You begin with the keyword, class. This is followed by the name of the derived (inherited) class. Then you have a space and the word, extends. Next you have a space again and then the name of the existing class. The existing class is called the base class. We say the derived class is inherited from the base class. After the base class name is typed above, you have to describe (code) the derived class (new properties and/or new methods) within curly braces. The following code shows how you derive a class using the above-mentioned base class:

<?php

    class Calculator
        {
            public $num1;
            public $num2;

         function __construct($v1, $v2)
                {
                    $this->num1 = $v1;
                    $this->num2 = $v2;
                }
    
            public function add()
                {
                    $sum = $this->num1 + $this->num2;
                    return $sum;
                }
        }

    class ChildCalculator extends Calculator
        {
            public $fixedVal;
    
            public function squareAdd($answer)
                {
                    $finalVal = $answer * $answer + $this->fixedVal;
                    return $finalVal;
                }
        }


        $myChildObj = new ChildCalculator(6,7);
        $myChildObj->fixedVal = 5;
        $result = $myChildObj->add();
        $endResult = $myChildObj->squareAdd($result);
        echo $endResult;

?>

The base class calculator has two properties and one method (and also the constructor method). The derived class has one property and one method. If the derived class does not have its own constructor function, then it inherits the constructor function of the base class. The value that will be assigned to the property of the derived class will be a fixed value. The method of the derived class, squares its argument and then adds the value of its property to the square.

Let us look at the last code segment: The first line instantiates a derived object from the corresponding derived class, using implicitly, the constructor of the parent (base) class. In this code, no object has been instantiated from the base class; that is not necessary as the derived class inherits all the members of the base class. The next statement assigns the value, 5 to the single property of the derived instantiated object. The statement, which comes after, calls the inherited add() method of the derived object and the values of the inherited properties are summed. The return value of the inherited add() method is assigned to the variable, result.

The statement after, calls the squareAdd() method that belongs sorely to the derived object (class), sending the returned value (result) of the inherited method as argument. The returned value of the squareAdd() method is displayed by the echo statement, next.

So a derived class has inherited members that it can use. It can also have its own new members. If you want a derived object, instantiate it from the derived class. If you want a base object, instantiate it from the base class. The instantiated derived object and the instantiated base object are normally independent, unless you force them somehow to be dependent; they are related though.

You can still derive a class (and corresponding object) from a derived class to have a grandchild. In this case the former child class becomes a base class and the new child class becomes a derived class. The chain can grow downward.

Note: in a class definition, whether it is a base or a derived class, you access an argument in a method definition using the parameter variable; you do not need $this for that; $this is used when you are accessing a property of the class and not the parameter of a method (function). That is how the variables, $v1, $v2 and $answer have been used above.

What is inherited from Base Class?
The members (attributes and methods) of base class are inherited. A constructor or destructor is inherited if a child class does not have its own constructor or destructor, respectively. Now, these are quotations concerning the inheritance of constructor and destructor, taken from the PHP specification: “Parent constructors are not called implicitly if the child class defines a constructor.” and “Like constructors, parent destructors will not be called implicitly by the engine.”

That is it for this part of the series; we continue in the next part.

Chrys

Related Links

Major in Website Design
Web Development Course
HTML Course
CSS Course
ECMAScript Course
PHP Course
NEXT

Comments

Become the Writer's Fan
Send the Writer a Message