Answer the question
In order to leave comments, you need to log in
How to think in objects?
Hello! Constantly, almost in all manuals and in the comments on the forum they write that you need to think with objects in OOP. And an example is given by type: there is a ball, it has a radius, mass, and so on. (these are properties). The ball can roll, fly, etc. (these are methods).
But, how to translate this into programming... I need to create a class of comments and then a stupor... After all, of course, if you think in objects, then there should be one comment...
A comment has properties: author, text, rating.
And methods: display comment, delete comment.
But what to do with such methods as: Getting comments for a photo, displaying the entire list of comments, and so on? After all, throwing them into a separate function is somehow not rational and probably not right ... so you can lose the whole code. How can all this then be combined into a more correct structure?
Answer the question
In order to leave comments, you need to log in
Gentlemen, there is no need to complicate anything - everything is outrageously simple))
When you say Comments - this is your object.
When you say CommentsAnd - this is already an array of objects: a container, a collection - I don’t know what exactly you are programming on, I will call it a list (it’s wrong to call it an array).
So, we have a Comment List - this is an object that contains inside itself (in one of its properties) many objects of the Comment type and provides access to them as an array - by indexes. But unlike an ordinary array, which is a store of a finite number of objects (unless you're using a scripting language, in which case array and list are synonymous), a list is just an object that can have methods like: add, remove, select according to certain criteria, and so on. It can also have its own properties, for example: a default filter, the maximum number of list items, and the like.
Consider object models (classes) as a description of a system (a fragment from the real world). Other systems can communicate with such a system: to report something, to ask for something to be done or to report.
Teach and learn to apply design patterns .
www.phptherightway.com/pages/Design-Patterns.html
designpatternsphp.readthedocs.io/en/latest
But what to do with such methods as: Getting comments for a photo, displaying the entire list of comments, and so on? After all, throwing them into a separate function is somehow not rational and probably not right ... so you can lose the whole code. How can all this then be combined into a more correct structure?Every programmer has asked this question.
Paradox: OOP was born to bring the world of code closer to the real world. And here is a person from the real world asking how to think OOP.
But what to do with methods such as: Getting comments for a photo
Getting comments for a photo - in the photo class ( Photo.getComments() )
Displaying the entire list of comments - can be in the collection of comments ( CommentsCollection.findAll() which will return an array of comments [Comment, Comment, Comment, ...])
Thinking in objects will come with practice. For now, consider an object as an entity where you can store data and any functions that will interact with these and data, or use them to receive other data from other objects.
Also, do not be afraid to divide everything into many classes and methods - that's right! Maybe it's too early for you, but read Wikipedia about the principles of SOLID .
If you think of objects in the context of the domain model, then the methods of an object must change the internal state of this object. Accordingly, your assumption that the comment should have display and delete methods is not entirely correct.
Believe me, in one post it is impossible to describe how to build an application architecture. But you can understand the basic principles of design by reading the book Vaughn Vernon - Implementing Domain-Driven Design. But these principles are popular in the enterprise segment with complex business logic and large budgets, mainly in the Java language (high-quality, expensive, slow). Typical Internet applications for the most part use a different approach, where objects are more like a container for functions, united according to the "so the developer decided" principle, those who know about solid / grasp a little better, those who don't - a little worse, but in general it's an approach that's fast, cheap, lower quality, and generally not that different from procedural programming.
According to the principle of matryoshka. One object can contain another object, and that one can contain a third object, and so on.
Those methods that you have not got into the structure clearly do not apply to Comments. You need to create other objects, Comment Lists, Photo, and those in turn will be part of other objects, more "larger".
The object must control itself, change its internal state, but not affect the external world. If you need to influence something external, it means you need another object, so you shove your object there and this is "external" and conjure like a chemist.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question