Answer the question
In order to leave comments, you need to log in
When to use OOP?
Good afternoon, ~3 years I wrote in PHP exclusively in the OOP style, then I switched to JS + TypeScript, where almost everything is in the procedural or functional style (without OOP in general, I'm already confused how it's called correctly).
And then looking back, I see that most things can be described with the OOP approach and FP, but how will it be right, when and what to use?
I approached the choice in terms of:
I need an object that will hold the state/data, and are there general operations on that state?
Do we use OOP goodies (inheritance/abstraction/polymorphism)?
class PageController {
public actionIndex() {
// что-то
}
public actionDelete() {
// что-то удаляем
}
}
Answer the question
In order to leave comments, you need to log in
OOP is not only when you take some entities from the subject area and wrap each one into an object that can do something. It's more of an approach to code organization. You divide the task into subtasks, and the data into separate parts, abstract the details within the objects. This reduces the complexity of the architecture. Theoretically, any program can be written inside one huge function with a bunch of gotos. But no one does that because it's impossible to maintain and incredibly hard to write. OOP is a logical continuation of procedures. Now you not only abstract some parts of the program in one place, but now also the data along with them.
I need an object that will hold the state/data, and are there general operations on that state?
Each approach/paradigm has some purpose. If you understand this purpose and really want to use its advantages and disadvantages, then use it. If you don't understand, use another approach whose purpose you understand. If there are no such approaches, use generally accepted ones.
ps
Any code can be rewritten for any approach.
For comparison, procedural and OOP are like an old Soviet garage and a modern one.
Procedural code - a lot of things (including unnecessary, obsolete), it seems to work and everything is there, but without a bottle you can't figure out what's where:
OOP - everything is laid out on the shelves, order, each drawer for a strictly defined tool and signed:
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question