2

I want to add some data form child class to parent class using the same method and also want to retrieve the data. Please check the example code that will help you for better understanding.

class HTML{

    public function add_control(){

    }

    public function all_controls(){

    }

}

class Control1 extends HTML
{

    public function register_controls()
    {

        $this->add_control([
            'name' => 'a',
            'label' => 'A',
        ]);

        $this->add_control([
            'name' => 'b',
            'lable' => 'B',
        ]);
    }

}

class Control2 extends HTML{

    public function register_controls()
    {

        $this->add_control([
            'name' => 'c',
            'label' => 'C',
        ]);


    }
}

(new HTML)->all_controls();

Sample Output ['a','b','c'] I hope you got my point.

6
  • You have said what you want, but there doesn't seem to be any attempt to solve your own problem. Commented Apr 8, 2020 at 16:16
  • 1
    That's not really how inheritance works. If you have an individual instance of your HTML class, which is what it looks like you want, it doesn't have any knowledge of Control1 or Control2 instances (or even that those subclasses exist). You might want to look at something like the Composite Pattern Commented Apr 8, 2020 at 16:18
  • @NigelRen , I actually don't know how to solve this problem the reason why I asked. Commented Apr 8, 2020 at 16:21
  • Thank you @iainn , let me check if that can solve my problem Commented Apr 8, 2020 at 16:23
  • You can try using abstract classes and methods Commented Apr 8, 2020 at 16:26

1 Answer 1

1

I am assuming that your data is in a class variable. I didn't quite understand your point. It is not really possible to get the data of the child... It would be possible with a static class variable but then you won't be able to use $this in the add_control($data) function. All data will then be stored in one variable which is shared by all the three classes. This is my approach to your problem, I hope it solves your question.

<?php

class HTML{
    protected static $data;

    public function add_control($data) {
        self::$data[] = $data;
    }

    public function all_controls() {
        var_dump(self::$data);
    }

}

class Control1 extends HTML
{

    public function register_controls() {

        $this->add_control([
            'name' => 'a',
            'label' => 'A',
        ]);

        $this->add_control([
            'name' => 'b',
            'lable' => 'B',
        ]);
    }

}

class Control2 extends HTML{

    public function register_controls() {
        $this->add_control([
            'name' => 'c',
            'label' => 'C',
        ]);
    }
}
$html = new HTML();
$control1 = new Control1();
$control2 = new Control2();
$control1->register_controls();
$control2->register_controls();
$html->all_controls();
?>

Edit:
After the hint of Markus Zeller... you can also make the all_controls() function static. Then there will be no need to create an object of the class HTML. If this will suit your problem.

class HTML {

public static function all_controls() {
      var_dump(self::$data); //or any other echo method
}

}
//other stuff...
$control1->register_controls();
$control2->register_controls();
HTML::all_controls(); //no object of HTML necessary
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for writing out my comment as an answer. Hint: You can make all_controls() static as well, then no $html instance is required at all.
yes, you're right. I thought I'd leave it at the static variable, since he apparently wanted an instance of the HTML class (see last line of his code)... I will update the code a little.
Another improvement suggestion: Instead of array_push(HTML::$data, $data); use self::$data[] = $data;. 1) Having classname itself is not good when renaming the class, 2) Adding to an array using [] syntax is more efficient.

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.