O
O
OneTwoThreeFourFive2019-09-09 20:40:54
PHP
OneTwoThreeFourFive, 2019-09-09 20:40:54

How to create a child class that is visible in the parent in PHP?

Hello. I'm making a WordPress plugin that has a control panel with buttons. I'm trying to use OOP and create a class that is responsible for creating buttons. And I create child classes that add these buttons. That is, to be able to make an addon that would add other buttons. When I create an instance of the main class (creates buttons) and child (adds buttons), then these are 2 different objects. How to make the main class aware that there is a child class and take data from it to create buttons. I suppose that in the main class, create an array to which elements (buttons) are added using child classes, and in the main class, through foreach, display the buttons on the page. I'm just starting to learn OOP, if this is all wrong, please advise the right solution.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Demian Smith, 2019-09-09
@search

Look at it this way: each button implements the `Button` interface. In addition, each button can have a common parent. This is if you so desire. It's not obligatory. You also have a `Panel` class that stores a collection of objects that implement the `Button` interface.
In general, the topic of OOP is, to put it mildly, not an easy one, and there are not so many people who wrote something decent the first time (I have not personally met such people). But the topic itself is interesting and worth it to dive into it. Now I want to apologize in advance for the advice, which sounds very arrogant, it is not to somehow offend you, but rather to help you quickly understand the issue: memorize the principles of SOLID and repeat them every day before going to bed. After about six months or a year of daily repetitions and training in OOP, these principles will begin to make sense to you and then your OOP code will look like the code for which companies are willing to pay the salary of a middle-level programmer.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question