Ok, let me answer this one by one ...
1. Is it worth doing something like this
Yes and no. A good idea is to split the helper functions into your own classes. It keeps the "sphere" of each class rigidly defined, and you do not get hacked. However, do not set the method static just because you can. The request method is to simplify your life by managing the connection, so why do you want to lose this benefit?
2. They are harder to test
They are not harder to verify. State-dependent static methods are harder to test (to access static member variables or global variables). But static methods in general are as easy to test as instance methods (in fact, they can be simpler, since you don't have to worry about importing).
3. Extension of classes
This is an urgent problem. If you put String_Helper::foo() in the class itself, you will run into problems. But an option would be to set the name of the string helper as a class variable. That way you could do {$this->stringHelper}::foo() (note only PHP 5.3). Thus, to override a class, all you have to do is change the class of auxiliary rows in this instance. The Lithium structure makes this a lot ...
4. Global registry
I would stay away from this. You basically just make each class a singleton without using it. Testing will be a nightmare, because now you are dependent on a global scale. Instead, I would create a registry object and pass it to classes through the constructor (Dependency Injection). You do the same thing anyway, since you have storage for objects / classes, but you are no longer dependent on the global scope. This greatly simplifies testing.
Usually
When you look at such things, I like to stop when I come across such questions. Stop, sit down and think: "What is the actual problem I'm trying to solve?". List the problem explicitly. Then pull out our proposed solutions and see if they really solve them. If so, think about the future and if these solutions are indeed supported in the long run (both from the point of view of fixing errors, and taking into account additions to functions). Only if you are happy with both of these answers should you think about it. Oh, and donβt forget, Programming is not about making the hardest, smartest or most amazing decision. It's about making the simplest solution that solves the problem ...
I hope this helps ...
Good luck
ircmaxell
source share