1

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?

freaky
  • 990
  • 3
  • 17
  • 44
  • 3
    When you extend a class you can just refer to `$this->data` and `$this->data2`. You only use `parent::` when calling a parent method if you've overwritten it in the child class. That said, it seems that you're not understanding OOP that well. Why are you loading a class with the same inheritance *inside* of the parent class? Just create a new `Child_Class` instead with the info you need. – h2ooooooo Mar 08 '16 at 08:39
  • Do you have an example because it's not populating the variable like this. – freaky Mar 08 '16 at 08:40
  • [Here you go](https://eval.in/532429). Note that we're calling the parent class in the overwritten `getData` and `getData2` methods. **Edit**: [If you prefer to use the constructor to add data, that isn't an issue either](https://eval.in/532430). – h2ooooooo Mar 08 '16 at 08:43
  • 2
    A tip: practice some more with OOP PHP, this will help you understand what happens better. Just take some time and follow this course: https://www.codecademy.com/courses/web-beginner-en-bH5s3/0/1 – Patrick Aleman Mar 08 '16 at 08:47

2 Answers2

2

"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:

Community
  • 1
  • 1
VolkerK
  • 95,432
  • 20
  • 163
  • 226
  • Thank you for your answer. If I have several child classes, does the execution of each child class one after this other will allow to transport the $data var in each child and updating it? Or do I need dependency injection to tranport and update the $data var? – freaky Mar 08 '16 at 09:04
  • `If I have several child classes` - several derived classes like `A extends My_Class {} ... B extends My_Class { } ....` or several instances of `Child_Class` like `$a = new Child_Class; $b = new Child_Class; ...`? Anyway, on that level it works either way. – VolkerK Mar 08 '16 at 09:13
0

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.

mohas
  • 1,911
  • 1
  • 16
  • 20