Elegant practices on "random utility functions" [closed]
How do you all organize your random functions to improve a language's functionality outside of OOP classes (global functions)?
I've seen libraries, but I'm still not sold on this being a good solution, especially if you don't have enough functions. I'm specifically interested in how people organize random PHP and JavaScript functions.
I try to avoid declaring functions in the global namespace altogether. The very rare occasions where I do this, is when adding userland implementations of functions that are not in my version of PHP, for instance
if(false === function_exists('lcfirst'))
{
function lcfirst( $str ) { /* ... */}
}
Functions like this could go in a compatibility.php which would be included in a bootstrap file, so they are available throughout the application and the check to function_exists
makes sure I don't run into issues once the PHP version has native support for the function.
For all other functions, I'd try to see whether they cannot go onto a dedicated object first. Usually, "random" functions are just misplaced. Have a look at which objects use your utility functions and then see whether you can move the methods there. Maybe there is a superclass waiting to come out. Also see Information Expert pattern.
If there is no objects these methods can go on, you can still group them in a static module with the name Utils in a unique namespace, so they don't clutter up the global namespace. This way, you can be sure you are not clashing with other third party functions in the global scope.
Prior to 5.3, I'd group them following the PEAR naming convention and prefixing class names following your folder structure, for example if the module was in com/mattmueller/utils.php
, you'd use
class Com_MattMueller_Utils
{
public static function something($a, $b) { /* ... */ }
}
As of PHP5.3, we've got real namespaces and you can do
namespace com\mattmueller\Utils;
class Utils
{
public static function something($a, $b) { /* ... */ }
}
In Javascript you don't have namespaces but can easily simulate them by adding the functions to an object, e.g.
// JavaScript
var com = (com) ? com : {};
com.mattmueller = {
'Utils': {
'something' : function(a,b) { /* ... */ }
}
};
The common frameworks usually implement functions for creating namespaces as well.
I generally reserve a functions.php
or common.php
for all of my weird functions that should have ideally been in PHP in the first place. (Meaning, not at all specific to my project).
This can be something like making a standard function extend to multi-dimensional arrays, or something else that fits in that category.
When I change projects, I just copy that file over to the next project, and it can easily go with me wherever. Then I just make sure it is loaded in my load script, and I have successfully extended the language.
For project specific things, I keep a Misc
class that contains the really weird function calls that are, at the same time, project specific.
For Javascript functions, I can imagine the same thing can apply. If you want to create a functions.js
or a global.js
type file, you could probably use the same logic.
I allways use a helper class where I can put all my non-OOP code, LOL. I mean that it's the propourse of helpers still being OO and have methods instead functions, with the advantage that you can organize your functions in diferent helpers. Like StringHelper, DBHelper, etc.
For Javascript, I've found that the first choice should be to integrate my utilities into jQuery. It's as easy as writing any other sort of function, and when things get more complicated it's great to be able to leverage the paradigm that jQuery imposes over everything (and over all my other page-specific code in the site).
In JavaScript, make a new file and group them under an object
global.js
:
/* Function definitions */
var myFunctions = new Object();
myFunctions.func = function () {
alert("hello");
}
Same idea can be used for PHP. With this, you don't need to worry about conflicts in naming conventions when your program grows bigger.
精彩评论