I could not resist also having to post an answer.
Is this a good practice?
It has been said by @bigown that "good practice" or "bad practice" is sometimes simply a modinha that everyone wants to follow.
But first we should ask ourselves why use each thing.
I would not say that it is a "good practice" or "bad practice" to use a class full of static methods and properties, but would say that this is a misuse of the resource .
Most of the time, I have seen things that were done with static classes that could have been solved with the creation of various functions.
I do not usually follow something just because everyone says it's good or bad, but I like to evaluate each pattern and the need for each feature.
Let's think, is it really necessary to create a class, rather than static methods, just to make it a "function repository"?
If you want to be a bit critical (as I am), you'll notice this in the following code link.
illuminate / support / Arr
This is a class from the Illuminate\Support
library, from 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 realize that it has each static method with the need to pass array
as a parameter to work with it.
I love using the Laravel Framework, but I do not need to agree with everything that is there. Although I use this class in some parts of the systems I've developed, I realize that structuring a static whole class just to work with arrays
is to misuse static methods.
If you look closely, you could do a function for each method of this class, rather than creating a class just for that.
Another thing I noticed is that no static property is used, not even to save a state. What, in my analysis, makes still more useless a use of a class with static methods to work with arrays
.
What I am going to talk about next is not a standard, but only my analysis when developing classes for libraries.
Let's go to the examples
When it comes to PHP, it does not 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:
Oh, but I wanted to separate the functions within a specific namespace, as you do with the classes. That's why I did it.
In PHP it is possible to set 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, such as;
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 only filling a static method class, but other stuff) because they have no idea what they're doing.
So what is the static method for?
I often observe how language uses things. And the way that PHP usually uses static methods is to create the instance of the class itself, since the language does not have the ability to use multiple constructors.
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 passes, as in the case of a format interpretation, you can do this:
$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 I see that is very common is to use factories
, to facilitate the instance of a given class, given the level of complexity and dependencies of it.
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 static methods do not just do what is being done above, but in my point of view, in an OOP structure it makes more sense to use a static method to aid in the inner operations of the class itself than to fill a class just to have an "organization".
The above examples are intended solely to demonstrate that what is important is the purpose of the appeal. I'm not creating any pattern to be followed blindly, but just trying to demonstrate that in some cases there are features being used that usually leave something more complicated / confusing than using others.
At the end of the day, what really matters is knowing well what the purpose of each thing is for not doing nonsense: p