Is it wrong to have a class with methods only?

4

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

    
asked by anonymous 05.05.2018 / 00:36

2 answers

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 that a code is wrong, just classify it as a " face, why code? " code.

Bytheway,thebestwaytoclassifythequalityofacodeisbythetemporaldensityofwtf,thatis,howmanywtfperminutethereaderdropswhenreadingyourcode.

AsManierocommented,alreadyusingobjectorientationinPHPwillsurelygeneratesomewtf.Thisisaverycommontopicofdiscussionhereonthesiteanditisnotdifficulttofindtheargumentsthatsupportsuchaposition.However,ifwellused(consciously),thenumberofgeneratedwtfwillbeverysmall.Aswellasusingdisguisedfunctionsofmethodsinaclass,whichwillinfactbeanamespace,justbydoingsoitwillcertainlygeneratemanywtfs,forthesimplefactthatthereisnoreasontodoit(realizethatIdonotsayitiswrong,Isaythereisnoreasontodoso).

Somuchsothattherearerelativelyfamiliarframeworksinthecommunitythatareimplementedentirely(oralmost)withdisguisedfunctionsofmethods,toquote Code Igniter . It does not mean that it is good or bad, but in doing so, it will have its peculiarities that it will be up to you, as a developer, to evaluate whether it is worth it or not to use it. The same thing happens when you're implementing something like this.

If you do not really see advantages in using the class, I would say not to use it. Clearly you need to define functions and functions exist for this. If you want to resolve function names and avoid any possible conflicts, group functions within namespaces . This way, at least you will be keeping the semantics in your project and leave classes for when you really need classes.

Note that just using a class does not mean that you are using object-oriented. This paradigm goes far beyond that.

    
05.05.2018 / 02:00
4

Wrong is a very strong word, but I do not 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 inside a namespace , it makes more sense than classes. I would not do that either.

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, even though I think it's the only one that makes sense.     

05.05.2018 / 00:42