Is it bad practice to use only static methods in a class?

Asked

Viewed 2,755 times

15

I was studying further sinking the OOP, learning more advanced concepts such as Polymorphism, Override, Classes and final methods, abstraction, namespace and etc...

I learned about static methods, where it cannot be accessed by the object (operator $this), and obviously cannot be instantiated by the object using the operator ->, but I was looking at some projects on Github and saw that some of them, they used all static methods, everything in class was static, from their attributes to their private and protected methods, the guy used the operator self:: to be able to reference and use the class methods and their attributes, so if you wanted to use the class, just use the namespace of hers, something like use class\ProjetoDoCara; and call the only public method that does the whole service ProjetoDoCara::Operar();.

But then a question arose, if this is good practice, it does not escape the rules of OOP?

  • 2

    That is my opinion. The soul of good practice in programming, independent of language and has three fundamental points that are the readability of the code, the productivity gain and the reuse of the code, in an easy way, by another person. I do not consider the case that you experienced as bad practice, since the instantiation of a specific class does not have so much relevance in the project.

  • 2

    There are those who use only to not create an object (practicality), many methods or static attributes may be symptoms of this problem.

  • 1
  • I was going to ask this question today, but by researching a number of articles I got it better "when we should use", what goes of the need of each in the design of the specific class. Maybe I will try to post something later.

6 answers

15

I decided to answer because the current answers do not talk about PHP that is the question, and the accepted talks about variables and not methods, will understand the AP.

Good practice

First I start with what I always say: this business of good or bad practice is a simplified way that people have invented to give (not to use a more forceful word, but who is aware of what I am talking about) rules that even they themselves know well because they are talking. You have to do the right thing in every situation. So you have to understand the basics and stop looking for cake recipes. This doesn’t work well.

OOP

If you’re gonna talk good practice, why don’t you ask yourself Using OOP in PHP is good practice?. There I give several reasons and hope that they are enough not to look pure cake recipe.

In fact static classes do not conform much to OOP. But so what? Why do OOP? You have to justify to adopt anything? Using unused is not a reason. If the problem does not ask for OOP to be solved well, you do not need to follow this.

If you will only have static methods, why not use common functions? Is there a reason to create a class? Always ask yourself. And remember that you are using PHP, the application runs in short pieces for very little time. Complicate the design brings a overhead unnecessary that can make a huge difference in this execution model.

But if you do, there is nothing wrong, I just think it’s unnecessary. No one can cite a real problem that has no solution that it can cause. Of course, if the problem asks for an instance, then it’s something else, but then you’re using the wrong tool for the problem. That’s something else.

Some of the problems reported in the other answers simply don’t happen in PHP, or only happen if the person doesn’t know what they’re doing. Some even make sense. Some answers say it’s problematic, but it doesn’t say why it is, as is typical of those who love "good practices".

Static state

Has state in a static class may be a problem, but in PHP it is rarely. As long as you don’t abuse it, if you know what you’re doing. I’ll repeat it because a lot of people think PHP is. NET, C++, Java, etc. No, the execution of PHP is ephemeral. It is a language of script and she shines doing it. It’s a shame when people try to impose models of other languages on her. And it’s worth remembering that no one uses thread in PHP, even if it can. It’s rare to make sense of the type of application PHP handles. And include is your friend. Don’t make him an enemy.

There are even cases to do OOP, there are cases to create a class, there are cases to do everything static (even without classes), there are cases for Singleton. People need to look for things that simplify their lives, not complicate.

Static method

Just get it right why use a static method (is another language, but the basis is the same) and use a static class (more general), also here. What is its function. Know that encapsulating a static method has its advantage, but it may only need a normal function.

Completion

You can say, "but you didn’t say when to use it or not". Yes, because you can’t do it. Without a very specific case I don’t know whether to use static method or not.

I didn’t see the project quoted in the question, but looks like be a case where static method was the best tool. If it needed to be a class I don’t know, maybe by the type of project.

6

It is bad practice to use only static methods in a class?

No, it’s not bad practice.

A completely static class can make sense even in a very well organized code.

Completely static class evades OOP rules?

Completely static class does not escape any OOP rule, it just doesn’t fit like OOP.

See: OOP implies talking about objects, and a completely static class (which has only static members) will never be an object in the sense of OOP.

But nothing prevents you from broadly using the most important OOP concepts in your project and eventually, when it makes sense, using a completely static class.

Now, if code uses much more static classes than objects, it will hardly be framed as an object-oriented project. It will probably be more to procedural. But in this case it does not mean that it hurt rules - it just means that it is not OOP.

6

I couldn’t resist also having to post an answer.

This is good practice?

It has already been said by @Maniero that "good practice" or "bad practice" is sometimes simply a modinha that everyone wants to follow.

But first we should ask ourselves why to use each thing.

I wouldn’t say it’s "good practice" or "bad practice" to use a class full of methods and static properties, but I would say it’s a misuse of the resource.

Most of the time, I’ve seen things that were done with static classes that could have been solved by creating various functions.

I don’t usually follow something just because everyone said it’s good or bad, but I like to evaluate every pattern and the need for every resource.

Let’s think, it’s really necessary to create a class, full of static methods, just to make it a "function repository"?

If you want to be a little critical (as I am), you will notice this in the following code link.

Illuminate/support/Arr

This is a library class Illuminate\Support, of Laravel. It is called Arr because it is a class created to do various operations with array.

If you look closely at this class, you will notice that it has each static method with the need to pass the array as a parameter to work with it.

I love using the Laravel Framework, but I don’t have to agree with everything that’s there. Although I use this class in some parts of the systems I’ve developed, I realize that structuring a whole static class just to work with arrays is to misuse static methods.

If you look closely, you could make a function for each method of this class, instead of creating a class just for that.

Another thing I noticed is that no static property is used, not even to hold a state. Which, in my analysis, makes it even more useless to use a class with static methods to work with arrays.

What I’m going to talk about next is not a pattern, but just my analysis when developing classes for libraries.

Let’s go to the examples

When it comes to PHP, it doesn’t make sense to have static classes like this:

  class Util {

       public static function tratarTexto($string) {
              // faz o tratamento

             return $string;
       }
 }

Instead, something like:

 function tratar_texto($string) {
      // faz o tratamento
       return $string;
 }

But then someone will say:

Ah, but I wanted to separate the functions within one namespace specific, as is done with classes. That’s why I did so.

In PHP it is possible to define namespaces, not only for classes, but for functions and constants as well.

So it would be perfectly possible to organize your code in other, more readable ways, like;

namespace System\Core;

const MY_CONST_VALUE = 42;

function my_function ($str) {

}

What I realize at the end of the day is that a lot of people do things like that (not just fill a class of static methods, but other things) because they have no idea what they’re doing.

So what’s the static method for?

I often observe how language usually uses things. And the way PHP usually uses static methods is for creating the class instance itself, since the language does not have the feature of using multiple constructs.

An example is the class DateTime.

You can use it like this:

$date = new DateTime; // Date(object)

And if you need to initialize from other parameter passages, as in the case of a format interpretation, you can do so:

$date = DateTime::createFromFormat('d/m/Y', '20/08/2009'); // Date(object)

So I can conclude that one of the purposes of the static method is to be able to create an alternative form of instantiation of the class itself.

Another way that I see that is very common is in the use of factories, to facilitate the instance of a given class, given the level of complexity and dependencies of the same.

Example:

// Forma complexa
new Controller(new Response(), new Request())

// Forma simplificada

Controller::factory(); // Os parâmetros Response e Request são passados internamente

Remembering that the static methods are not limited to doing what is being done above, but in my view, in an OOP structure it makes more sense for you to use a static method to assist in the internal operations of the class itself than to fill a class of it just to have a "organization".

The above examples have only the purpose of demonstrating that what matters is the purpose of the resource. I’m not creating any pattern to be followed blindly, but just trying to demonstrate that in some cases there are resources being used that tend to leave something more complicated/confusing than using others.

At the end of the day, what really matters is knowing what the purpose of each thing is to not screw up :p

  • In PHP, functions have global scope, so organizing functions in namespace will not work.

  • What do you mean, young man? I don’t understand your question @Evertondarosa

  • you wrote In PHP it is possible to define namespaces, not only for classes, but for functions and constants as well., but the PHP manual contains the following information: All functions and classes in PHP have the global Scope - they can be called Outside a Function Even if they Were defined Inside and vice versa. That’s why I said to organize the functions with namespace won’t work, though, if I’m not mistaken, PHP will not trigger any error or warning about this.

  • @Evertondarosa have you ever done tests? Because, in the ones I did, it worked. And you have to see if the documentation has been updated, since version 5.3 here several changes have been made.

  • 1

    Some time ago I tried to define functions over a namespace and it happened exactly as the manual said. I can’t remember which version of PHP it was. Today, I did new tests and the behavior changed: functions defined under a namespace are now isolated from those defined in other namespaces and in the global scope. In practice, we can have functions with equal names, but in different scopes (tested with PHP 7.2.2). I have not tested the function definition within function, but if you define the functions of my background(); Meunamespace1 my background() and Meunamespace2 my background(), everything will work.

3


Good afternoon, Cassiano José!

Using too many static variables is bad practice, yes. Some may argue that in some specific cases it is something usable, but in general, start by getting away from static variables.

One of the examples that can use static variables is for when it is a final variable, which will no longer have its value changed.

Think of the following case: You publish an application. net on the web (I will only talk about .net, but I imagine it applies to several other platforms), and then you put a certain object as static, let’s use as an example a Product. User 1, populates this object after selecting a Product for some particular operation. User 2 goes there and populates this same object to do another (or the same) operation. Okay, we have a conflict. User 1 will have a Product object with the information that user 2 set in this object.

I recently picked up a project that was occurring this kind of conflict, my simplest and most functional solution, I imagine, was to generate a Session with the information that was previously going to a static object. The Session is unique, so each user had their session stored there and could pass the information to another page of the system, for example.

So, that’s it, very careful with the static variables.

I hope I’ve helped.

Hugs.

  • 1

    It is, looking at this side really gets complicated a project that has classes totally with static methods, on an occasion like this that you mentioned, would really affect the system a lot! thanks for the remark!

  • 2

    In this cited example of conflict by two users, it does not occur in php, for example. Because each request is an independent instance. Nor should it occur in similar environment languages (web)..

  • Um, cool Daniel, I didn’t know that in Php this didn’t happen, interesting.

1

The use of static attributes and methods cannot escape its definition in object orientation, otherwise a bad practice will be configured, ie, a way to facilitate a given implementation or a branch break.

In object orientation, the existence of static attributes is directed towards common attributes among objects belonging to the same class in the modeling of a system, not causing a side effect on the definition of the OO system. For example, employees of the same category have, or at least should have, the same percentage increase at the end of each year, so it is fitting to have a single value for all objects.

In the same vein are static methods, must act on common interests in the class. As you mentioned that all methods were static, this is a way to make the object orientation paradigm more like the imperative, so you lose all the numerous advantages that the development of an object-oriented system can provide.

  • 1

    Excellent placement Leandro!

0

Looking at the concepts of OOP, yes, static classes hurt the norm and it would be better to use a Singleton to create a utility class.

Let me give you an example to help you see the problem. In my c++ code here, I found a static Brun variable in a class that I use to create all the threads of my program. Instead of ending with program with Exit(0) or something like that, I put Brun for false and all threads stop running, calling the destructors and ending the program without memory leak.

But in doing so, I am relinquishing the advantages of Object Orientation. I can’t determine the order of threads ending, I can’t make the inheritance and add a behavior at the end of each thread and so on.

When you look at an Open Source project, especially one in C++, it may be that it used the feature of a whole static class because the variables and methods in this case load together with the program and can go to the Stack instead of going to the Heap (which is not true in many languages). Or he preferred to do it without object orientation because he didn’t yet know a good way to do it, it would take him longer to do it like this.

Only after you learn how to use Singleton, you’ll realize that it’s a practical design pattern enough to maintain the object orientation advantages even in these situations and you’d rarely actually need a static method.

  • I also thought about it @Rodrigo Guiotti, who thought about doing optimizations, I even find it more practical to use the form: Classe::Método();, I don’t know I find it cleaner, only it really hurts the development in OOP!

Browser other questions tagged

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