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?

4
  • 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. Commented Mar 8, 2016 at 8:39
  • Do you have an example because it's not populating the variable like this. Commented Mar 8, 2016 at 8:40
  • Here you go. 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. Commented Mar 8, 2016 at 8: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: codecademy.com/courses/web-beginner-en-bH5s3/0/1 Commented Mar 8, 2016 at 8:47

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

2 Comments

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?
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.