Is it wrong to have a class with only methods?

Asked

Viewed 110 times

4

Is it wrong to have a class with only methods? Or would it be better to use namespaces to hold office?

  • 3

    What advantages can you see by implementing a class of methods instead of implementing functions?

  • It would be kind of for a way to group the functions

  • This would be more for a consequence than an advantage. Creating functions in a namespace would have the same effect, so it sure isn’t an advantage. Don’t misunderstand, but to get the right answer, you need to ask the right question. When you can answer the previous question, you will have what you are looking for.

  • @user8741696 Did any of the answers solve your question? Do you think you can accept one of them? Check out the [tour] how to do this if you haven’t already done so. You would help the community by identifying what was the best solution for you. You can accept only one of them. But you can vote on any question or answer you find useful on the entire site.

2 answers

4

Wrong is a very strong word, but I don’t know what advantage it expects to have in creating a lot of functions in a stateless class.

I would just create simple functions. It could be within a namespace, makes more sense than classes. I wouldn’t even do that.

To tell you the truth I already find it strange to use PHP classes in almost every case where they are used, but I know my opinion is controversial, although I think it’s the only one that makes sense.

3


Wrong is a very strong word (2).

I will implement my functions disguised as methods because ...

If you can finish this sentence, then it is at least acceptable to do so. There is no way to define a code is wrong, just classify it as a code "Man, why do it like this?".

inserir a descrição da imagem aqui

In fact, the best way to classify the quality of a code is by the time density of wtf, that is, how many wtf per minute reader loose while reading your code.

inserir a descrição da imagem aqui

As Maniero commented, already using PHP object orientation will surely generate some wtf. This is a topic of discussion quite common here on the site and it is not difficult to find the arguments that defend such a position. However, if well used (consciously), the number of wtf will be very small. As well as using functions disguised as methods in a class, which will in fact be a namespace, just by doing so will surely generate many wtffor the simple fact that there’s no point in doing it (realize that I don’t say it’s wrong, I say there’s no reason to do it).

So much there is even frameworks relatively known in the community that are implemented entirely (or nearly so) with functions disguised as methods, to cite Code Igniter. It doesn’t mean that it is good or bad, but in doing so, it will have its peculiarities that will be up to you, as a developer, to assess whether it is worth it or not to use it. The same happens when you’re implementing something like.

If you really don’t see the advantages of using class, I’d say don’t use it. Clearly you need to define functions and functions exist for this. If you want to resolve the function names and avoid any possible conflict, group functions within namespaces. This way, at least, you’ll be keeping semantics in your project and leave classes for when you really need classes.

It is also worth noting that just using a class does not mean that it is using object orientation. This paradigm goes far beyond that.

Browser other questions tagged

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