I am working my way through building a totally OOP platform to replace my previous procedural files. I am having trouble deciding how to implement utility functions though. Many, many classes or views (as in mvc views) require me to echo out text, or format text in a certain way over and over. I'd like to have a wrapper for functions like htmlspecialchars, var-dump, and other very often used functions, along with my own utility functions, but I don't know where to put these. Right now I have 3 options:
1) separate utility class composed of static functions
2) separate utility class composed of non-static functions
3) traits which I include on every class that that needs a utility
What is the best way to approach this? Instantiating the utility function class composed of non-static functions and then calling the appropriate method seems like the most OOP way, but it also will blow up my code's size quite a bit due to all that extra typing.
Single class instance managed by a DI container.
If you're using a templating language extend the templating language with the functions.
-> All of which frameworks such as Symfony 2 are a prime example of how to do things.
Sorry for the newbie question, but by "single class instance" do you mean a singleton? Or an instantiated instance of the utility function class before its method is called?
I would be referring to a class that is instantiated only once. However, not using a singleton pattern but having the DI container regulate instantiation to a single time. Once that is done using a DI container pattern to inject the helper as necessary into the given classes that require the dependency. That is typically how you would handle it in a robust, mature framework such as; Symfony 2. For templating helpers an option would be to extend the templating language with the utility functions. Symfony does that and Drupal 8 w/ twig adds even more helper functions specific to Drupal that way. Of course this all means you have a functioning, robust framework in place with a DI container pattern and template language… which again is why using frameworks such as Symfony 2 are useful rather than rolling your own lowe level, framework code. However, everyone seems to always want to write their own framework all time…
I like ircmaxwell's answer:
Well, the short answer is "nowhere".
The longer answer is: you shouldn't be using "misc" functions everwhere.
Escaping (SQL) should be near your DB access. Nowhere else should be doing that, otherwise you can create extremely complex problems.
Escaping (XSS) should be near your output (template). Nowhere else should be doing that, otherwise you can create extremely complex problems.
Sanitizing should be near your input. Nowhere else should be doing that.
Hashing functions (password) should be near your authentication logic. Nowhere else should be doing that.
Hashing functions (crypto) should be near the specific service that needs it.
Notice a pattern?
Well if its light-weight, just create a new object for a utility class each time you use it. If its heavy-weight, use Dependency injection instead. Never try static class/methods, they are very poor OOP practices, or more precisely procedural programming in disguise of OOP.
This topic is now closed. New replies are no longer allowed.