Lexx
Lexx

Reputation: 725

Multi level inheritance in PHP

I'm trying to access and modify data that is in a parent class which is a child of another class. I've a parent class

class GrandParent {
    protected $data = 1;

    public function __construct() {}

    public function getData() {
        return $this->data;
    }
}

Below is my first level child

class Child extends GrandParent {
    protected $c1Data;

    public function __construct() {
        $this->c1Data = parent::getData();
        $this->c1Data = 2;
    }

    public function getData() {
        return $this->c1Data;
    }
}

If I try to instantiate the Child class and do getData(), I get 2 which is normal. I've another class that inherits Child.

class GrandChild extends Child {
    protected $c2Data;

    public function __construct() {
        $this->c2Data = parent::getData();
    }

    public function getData() {
        return $this->c2Data;
    }
}

The problem is that if I try to instantiate GrandChild I and get the data I'm getting null. Is it possible to make my GrandChild class inherit $c1Data = 2 and work with it. I want also to be able to use the Child and GrandParent classes on their own and not be abstract.

Upvotes: 1

Views: 1320

Answers (2)

Rashid
Rashid

Reputation: 39

This is how you add two integers and display the result with Multi Level Inheritance in PHP.

    <?php 
    /*
    Inheritance:
    multiple classes
    Parent class/child class
    senior and junior
    child class extends some data or functions of parent class
    child class has its own functions
    child class can access all public and protected data and functions
    */
    //Multi Level Inheritance Every class extends other class
    //Parent Class
    class A{
        //data
        var $a;
        function setA()
        {
            $this->a=10;        
        }
    }
    //child class
    class B extends A{
        var $b;
        function setB()
        {
            $this->b=20;
        }
        
        
    }
    class Addition extends B{
        function add()
        {
            $this->setA();
            $this->setB();
            return $this->a+$this->b;       
        }
    }
    class Print1 extends Addition{
        function print()
        {
            $this->add();
            print("a=".$this->a);
            print("<br/>b=".$this->b);
            print("<br/>Addtion:".$this->add());
            
        }
    }
    //make object
    $obj1=new Print1();
    $obj1->print();

    /*
    Make Subtraction, multiplication and division classes and print the values as

    a=10
    b=20
    Addtion=30
    Subtraction=-10
    Multiplication=200
    Division:0.5



    */
    ?>

Upvotes: 0

u_mulder
u_mulder

Reputation: 54831

You're getting NULL because __constructor of a Child class is not invoked and that's why c1Data property is NOT SET. You should explicitly call for Child __constructor:

class GrandChild extends Child {
    protected $c2Data;

    public function __construct() {
        // here
        parent::__construct();

        $this->c2Data = parent::getData();
    }

    public function getData() {
        return $this->c2Data;
    }
}

Upvotes: 2

Related Questions