S
S
StasRomeo2018-10-31 12:22:33
PHP
StasRomeo, 2018-10-31 12:22:33

How to inherit?

Guys, why does inheritance fail? I study.
Here is my simple book class

$obj = new Book();

$post = array('name' => 'Книга1', 'author_id' => 10, 'price' => 1000);
$obj->create($post); //создаем книгу

$post = array('id' => 100, 'name' => 'Книга11', 'author_id' => 11, 'price' => 1111);
$obj->edit($post); //редактируем

$obj->delete($id); //удаляем
$obj->showAll(); //получаем весь массив книг
$obj->showOne(100); //получаем только одну книгу

And one private select(array $param) which, interacting with showAll and showOne, changes the query to the database.
Over time, new tasks and functionality appear (for example, add a cover, add annotation, etc.)
Where to add new code in a particular case? I can add it to the main class, or create an inheritor. Only here is what is here and from what to inherit I will not enter. All methods except select are so unrelated to each other. Everyone will make their own checks, queries to the database and that's it.
If the task was to display some other selectByAuthor(), then yes, it makes sense to inherit the same private select. But there is absolutely nothing to inherit.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
SwoDs, 2018-10-31
@SwoDs

Methods are not inherited, all parent methods are available to the class that inherits it.
In your case, I would simply extend the functionality of the Book class, since the work is related to it.
If you had a "Printed Products" class, then most of the methods would be there, and the classes Book, Newspaper, etc.
inherited from him. In these classes, there would be an implementation of only what applies to them, for example, a book has an author, but a newspaper does not ... something like this

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question