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
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.
– Sullyvan Nunes
There are those who use only to not create an object (practicality), many methods or static attributes may be symptoms of this problem.
– rray
Related: When we should declare a method as static? and What is the function of a static method?
– Math
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.
– Guilherme Nascimento