freaky
freaky

Reputation: 1010

PHP - pass variable between child and parent class

I would to know what is the best way to pass variable between parent and child class and updating this class all along the class is executed. For example I have this parent class which execute is child class inside it:

class My_Class {

   public $data;
   public $data2;

   public function __construct() {  
   }

   public function output() {
       $data['key1'] = 1;
       $data['key2'] = 2;
       $data2['key1'] = 'a';
       $data2['key2'] = 'b';
       $child_class = new Child_Class();
       $child_class->output();
       print_r($this->data); // only contains  key1 & key2, I want to get key3 and 4 also
       print_r($this->data2);
   }

}

class Child_Class extends My_Class {

   public function __construct() {  
   }

   public function output() {
       $data  = parent::$data; // want to get data array but it's empty
       $data2 = parent::$data2; // want to get data2 array but it's empty
       this->set_data();
   }

   public function set_data() {
       $this->data['key3'] = 3;
       $this->data['key4'] = 4;
       $this->data['key3'] = 'c';
       $this->data['key4'] = 'd';
   }


}

$class = new My_class();
$class->output();

Currently I execute the child class inside the parent class because I need to populate the main data of the parent class. This class will execute child class based on some variable.

What is the right way to inherit and assign variable from parent to child and child to parent. If I use dependency injection to retrieve the data in the extends class how can i assign the variable to the parent class?

Upvotes: 1

Views: 6069

Answers (2)

mohas
mohas

Reputation: 1931

My problem was a type, instead of $this->data I wrote $this->$data, the $ sign is only needed at the beginning of each statement.

My second problem was omitting the $this-> part when accessing variables on parent class thus creating locally scoped variables for parent class which was not shared with child classes, should have used $this->variable.

Upvotes: 0

VolkerK
VolkerK

Reputation: 96189

"Do you have an example" - here you go....

<?php
class My_Class {
    public $data = array('key1'=>1, 'key2'=>2);
    public $data2 = array('key1'=>'a', 'key2'=>'b');

    public function output() {
        echo "MyClass::output\r\n";
        print_r($this->data);
        print_r($this->data2);
    }
}


class Child_Class extends My_Class {

    public function __construct() {
        $this->data['key3'] = 3;
        $this->data['key4'] = 4;
        $this->data2['key3'] = 'c';
        $this->data2['key4'] = 'd';
    }

    public function output() {
        echo "Child_Class::output\r\n";
        parent::output();
    }
}

$class = new Child_Class();
$class->output();

prints

Child_Class::output
MyClass::output
Array
(
    [key1] => 1
    [key2] => 2
    [key3] => 3
    [key4] => 4
)
Array
(
    [key1] => a
    [key2] => b
    [key3] => c
    [key4] => d
)

see also:

Upvotes: 3

Related Questions