I have a class with 14 static methods and 4 static properties - is that bad?

I am writing a PHP class 450 lines long and contains 14 static methods and 4 static properties, as well as 6 constants (and private __construct() and __clone() ).

I am wondering if I am doing something wrong, is this a cool evil?

When you use a class, you always call one method, for example:

 MyClass::coolMethod(); 

and then you leave him alone, so it seems that it would be foolish to make him constructive?

Actually there isn’t much point in creating objects from it, because it is more like a tool that contains several methods that you can simply call directly.

Actually, of these 14 methods, 7 of them are publicly available - the rest are closed to the class that will be used.

+6
object oop php god-object
source share
5 answers

You should avoid static as much as global .

Statics gives you the same flaws that global variables give you. Whenever you use any class methods, you hard code the dependency on that class in the consumption code. The result is a less repairable hard code. This can easily be avoided altogether avoiding the statics and disciplined use of dependency injection.

You cannot introduce and pass static classes, therefore, for example, when you need to test them, you cannot imitate ( or, at least, only with some efforts ). It just hurts. Static methods are death to test.

Also, keep in mind that classes should only do one thing. They should have one responsibility . Go through your class to see if there are things that are better placed elsewhere to avoid writing the God Class.

+4
source share

It depends on the purpose of this class. If the methods are mostly inconsistent in terms of data, this is a perfectly acceptable solution to grouping functions (now methods). This is a very bad idea if you need to share values ​​between functions, as this will be a simpler list of functions grouped under a common name. Namespaces are another option, but if you are using the PHP version below 5.3, this is probably the best solution.

+3
source share

This is how to say: "I have a four-bedroom house. Is it bad?"

Static methods are neither good nor bad. Having fourteen methods is neither good nor bad. Having fourteen static methods, by extension, is neither good nor bad.

If in your fourteen methods you go long distances to simulate instances of objects or imitate inheritance, then something went horribly wrong. PHP allows you to instantiate and supports inheritance, so it would be foolish to try to mimic them any other way.

But if you just use your class essentially as a namespace, where all functions and data work together, but there are no separate instances of the class that can be dealt with, there is nothing wrong with that.

+2
source share

Not bad. However, with all of these static details, you might consider making this singleton.

Here is one singleton code that I use within the framework that I am creating. You can split it and make it the only public method for your class that returns a single version.

 class ClassName { function getInstance() { static $instance; if (!isset($instance)) { $instance = new ClassName(); } return $instance; } } 

You would use this by doing ClassName :: GetInstance () -> othermethod ();

Then the class can have a lot of personal values ​​and otherwise get all the nice things that you have with the object.

+1
source share

I would say no, this is not bad. In fact, this was the only way to fake specific behavior before. It was, for example, a way to fake namespaces. It was possible to encapsulate functions in static classes, instead of "freeing" them. So php developers are familiar with this, and this does not actually confuse most people. What you SHOULD try to do these days is to use the function of the β€œnew” PHP namespace and, if necessary, combine it with the singleton template if you really need to store the data in an object format. You could also have a β€œglobal” variable contained in your namespace, and from time to time this might work fine. But look at the namespaces and see if this is right for you, and after that, see if a singleton pattern can fit your specific needs.

0
source share

All Articles