Object-oriented PHP encapsulation

Asked

Viewed 279 times

6

I have two models User and Post.

When editing a post, I want to know if the user is the author of post (the post has an attribute user_id) to authorize the edition.

If I have a method within the class Post receiving an object User to check whether the User is the author of that Post, I will be respecting the object orientation paradigm?

I read that one class should not depend on another. This is the right way to do?

  • In my view, the biggest problem is to pass an entire object if the only thing you need is a number (id). I suggest reading that one question first

1 answer

6


It is difficult to say without further details. You would have to see the implementation of this, how it will be used and what might happen in the future to make the decision. I don’t even know if I should receive an object User.

If that’s what you want to do, do it, and see if that’s good in the future. And maybe you’ll find something else that a lot of people don’t realize: people do things for easier maintenance later, and they take the time to do that, and maintenance never comes. What I see most nowadays is a system that is virtually undeveloped, so you adopt a complexity for something that is not used.

OOP

But the first thing you should know is that there are several definitions of what object orientation is. And in all PHP benefits very little from this paradigm because it is secondary (some say not even a paradigm at all), because it serves to organize complex codes that relate in a complex way. PHP is a language of script, It’s practically an inherent microservice. It separates things so much that it makes little sense to use OOP in it. But she started to allow it because the industry in general is little informed of how things work and are very guided by marketing, which made her adopt the possibility of writing code like this "not to be out of fashion".

It makes more sense to use OOP in Hack, Java or C#.

But I can say that there is no magic formula to define what is right to respect OOP. And if you do, why do you need to respect if you do something that is appropriate to what you need? What problem does this cause? You have to make the decision because some problem is caused if you do it wrong. If you can’t define if it causes any problems, it’s not because it’s OOP that will help.

Among these controversies about what is OOP there are those who say that everything that concerns the object must be inside it, that is encapsulation. But each one has a vision of what this is all about. There are those who say that if something can be left out then it should be. I am in this class. That is, nothing to encapsulate the whole, but encapsulate the minimum. To such an extent that in some cases it should not even have a class. Much less with all the mechanisms they use.

Whatever the option, without mastering how to model things, without much experience, without having all the necessary information on the subject, and usually you don’t have that, unless you’re doing something you’ve done before and learned a lot from it, potentially you’ll get it wrong.

Learn to model and worry less about OOP. Even because they started using the term for two or more different things, there when you’re reading something you don’t know which version of OOP they’re talking to. And of course, whoever is saying that says that his is OOP, the rest is something else. Much of what they say is OOP is just modularization.

I realize that people have difficulty respecting even grammatical issues in Portuguese that they use all the time (I myself am like this), imagine in something that she does not always use, because the language that a person should speak when programming is the problem to be solved, It’s not even the programming language. People go through a lot of trouble on taxonomy and ontology or even dialectics, so it’s hard to get it right and know that you’re right.

And out comes this:

Fiat 147 detonado andando pelas ruas

Or even worse in OOP it happens a lot (and they don’t notice, especially in PHP):

Carro velho enferrujado aos pedaços semienterrado

Concepts that should be mastered and that have nothing (directly) to do with OOP: What are the concepts of cohesion and coupling?.

And see Is it correct to say that encapsulation aims at cohesion? Why?.

  • 1

    Maniero, your answer has made me very clear, thank you. " Mainly this paragraph": But what I can say is that there is no magic formula to define what is right for OOP respect. And if you do, why do you need to respect if you do something that is appropriate to what you need? What problem does this cause? You have to make the decision because some problem is caused if you do it wrong. If you can’t define if it causes any problems, it’s not because it’s OOP that will help.

  • 1

    As always someone does not like to hear that is PHP is not all that, OOP is not all that, and there negative. As the person has no argument he does not say why the negative.

  • 1

    I (even being a beginner) realized that there are some "purists" of programming. That such language is the best, such technology is the right... But I don’t fall for that hehe

  • 2

    @Maniero I am one of those who negatived, not because I disagree (I agree), but it seems to me that in the whole answer the only paragraphs that have a direct relation to the question are 5° and 6° the rest (and most) of the answer is focused on saying that "PHP is not all that, OOP is not all that". Although the links help a lot, they are also not what was asked (at least not if I understood the question), otherwise would have already been marked as duplicate

  • I know, and I know the other negative, too. I am glad that you decided to say why the negative and I do not agree with it because you yourself say that you have been answered and it is correct, to deny because you have other information does not seem appropriate, but it is your right. I know you do it for your convictions, it was not like the other who has evidence of bias.

  • 2

    @Maniero I think it is important to mention other problems in the answer, but it seems to me that, in doing so, he ended up blurring the subject of the question, maybe a paragraph or two with links to other answers from you (you have several options) talking about the subject would be enough. My negative is the blur, not the extra content

  • 1

    @Guilhermecostamilam yes, I understood that, but me I think it’s unfair, but at least it’s not stalking, okay. We keep disagreeing on that and life that follows, I appreciate the comment, I really respect you for that.

  • 1

    @Jhonny now you can vote on everything website too. Have you seen the [tour]?

  • 1

    @Guilhermecostamilam I think he answered what was pertinent and the blur you should consider with an additional relevant to something that usually only people with long experience with it will understand, ie it is pertinent, even if you do not understand at first ;) ... I am not saying that this is entirely right, but I think that to be sure of everything that has been said yet it is necessary to understand the common problems in this type of situation technically, which is usually not learned/understood in a few months, on the contrary, it takes a lot of experience.

Show 4 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.