views:

142

answers:

5

Hey all,

I was wondering how 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.

Thanks, Matt Mueller

+3  A: 

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.

Chacha102
+1  A: 

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).

Pointy
I would add that you can also extend the prototype for built in objects like Array as well
Goyuix
@Goyuix - generally speaking extending the native objects is frowned upon.
scunliffe
@scunliffe tell that to the Prototype guys :-)
Pointy
+1  A: 

I allways use a helper class where i can put all my non OOP code, lol, i mean that its 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.

useless
Me too (for PHP, at least). I have a folder called Util, with class files for StringUtil, ArrayUtil, etc. that are full of static methods. Combined with a good autoloading mechanism, it works very nicely.
grossvogel
+1  A: 

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.

JONYC
+1  A: 

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 utils.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 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.

Gordon
Awesome! Thanks for the insights. Yah I really wish I could use PHP namespaces but my hosting doesn't have 5.3 yet.
Matt
@Matt well, you can still go with the PEAR approach I noted or simply use a class name unlikely to clash, like `MMUtils` if you find the domain name based classname too long.
Gordon