A
A
Alexey Konovalov2018-06-28 23:38:06
PHP
Alexey Konovalov, 2018-06-28 23:38:06

How to get rid of repetitive code?

Hello! I have an Attachments class and a few other classes that inherit from it: ImageAttachment and VideoAttachment .
These classes are used by all modules on the site: "news", "posts", "comments", etc.
When I get a list of attachments for one of these modules from the database, then I need to iterate over the array of received data, determine the attachment type and add it to the list of attachments of any of the above modules ....
To do this, in each object class (news, posts, comments) there is an addAttachment method , which is essentially the same everywhere... How can I get rid of its repetition everywhere?
I had an idea to create some abstract class and all classes (news, posts, comments) inherit from it... Then I would have one addAttachment method , only in this abstract parent class... But it seems silly to me to inherit the Comments class from class AbstractAttachments .
I hope I explained it clearly ... After all, it is pointless to give the code - it is very large

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
alek_tost, 2018-06-29
@alek_tost

You can use traits and introduce a common interface for these modules.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question