tags:

views:

76

answers:

3

What is the best way to deal with "utility" functions in a OOP PHP framework? Right now, we just have a file with several functions that are needed throughout the system. (For example, a distribute() function which accepts a value and an array, and returns an array with the value distributed in the same proportions and same keys as the input array.)

I have always felt "dirty" using that because it's not object-oriented at all. Is it better practice to move these into various classes as static methods, or is that just a semantic workaround? Or is there just going to be a level in a framework where some stuff is going to fall outside of the OOP structure?

+1  A: 

I've always been more pragmatic about questions like these.

If you want to go full-OOP, you should obviously stick these into classes. However, these classes are only going to be container classes, because they don't really represent objects of any kind.

Also: using classes would require you to either have an instance of that class, using the singleton pattern or declaring every function static. The first one is slower (okay, might not be that much, but in a large framework things like that get large, too - especially in interpreted languages like PHP), while the second and third ones are just plain useless and simply an OOP wrapper for a set of functions (especially the third approach).

EDIT: Feel free to prove me wrong. I might be. I'm not too experienced and always saw it that way, but I might be wrong.

Franz
+1 good answer.
Byron Whitlock
Seconded. There is no point to turning everything into objects just for the sake of being 100% OOP.
Pekka
The benefit of pushing functions into an OOP wrapper and declaring them static is that, when you do call the functions, it's explicitly clear to readers of code where the functions are defined. I use this technique to, in effect, simulate namespaces.
jkndrkn
Hmmm... I see your point, @jkndrkn, but you can also do that with function prefixes.
Franz
@Franz But function prefixes are essentially the same thing as static methods in a class... except that now they are no longer consistent with the OOP code style of the rest of the framework. You're not saving yourself anything by using function prefixes instead.
Keith Palmer
Resources (granted, it's tiny - but there's a saying in Germany "You're not worth the Euro, if you don't care about the cent" - horrible translation) ;)
Franz
And since they are the same, there is no reason to use OOP either - thus I would go with functions, for the reason mentioned above plus that they were there first ;)
Franz
+1  A: 

I always think of utility functions as extensions of the standard php functions. They are not object oriented because you don't really get any benefit from making them OO.

Byron Whitlock
+2  A: 

I tend to make a Util() class that contains only static methods, has no attributes, and is not inherited from. Essentially, it acts as a "namespace" to a bunch of utility functions. I will allow this class to grow in size, but will occasionally split of methods into their own classes if it is clear that those methods are designed only to work with certain kinds of data or if it is clear that a group of related methods should be grouped into a class along with, perhaps, some attributes.

I think it's perfectly OK to deviate from purely OOP practices so long as the code that deviates is well-organized and is not creating architectural flaws in your system that make it harder to understand and maintain.

jkndrkn
I disagree with the first part - especially with the Util class. Just read my answer to see why.However, what you wrote after that got me thinking. If those sub-sets are not just a collection of static functions and nothing else, but for example have some combined properties (bad example, but the only one that comes to mind: view helpers in one class with the view being a class member - I hope you get the point). +1
Franz
Darn, I forgot my vote limit. Sorry. Four more hours ;)
Franz
Well i can actually see the use of a generic Util class to hold all kinds of little handy functions, keeps them clean, close together and under a common "namespace" namely the object. Indeed you need to define them static but hey, what's wrong with that?
ChrisR
Because there's no point in having all of them static. That's about the same as just having them as a set of functions, which have to be called with more characters. What's the class for in that case? You can have them "namespaced" and kept together in a special file with special prefixes, too. Still, here's my promised +1 vote ;)
Franz
Pardon, messed up timezones. 45 more minutes ;)
Franz