• 14

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191


File: /home/prodcxja/public_html/questions/application/views/question.php
Line: 191
Function: _error_handler

File: /home/prodcxja/public_html/questions/application/controllers/Questions.php
Line: 433
Function: view

File: /home/prodcxja/public_html/questions/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

Elegant practices on “random utility functions” [closed]

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.

  • 7
Reply Report
    • Awesome! Thanks for the insights. Yah I really wish I could use PHP namespaces but my hosting doesn't have 5.3 yet.
    • @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.

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.

  • 4
Reply Report

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.

  • 2
Reply Report
      • 2
    • 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.

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

  • 1
Reply Report

In JavaScript, make a new file and group them under an object


/* Function definitions */ 
var myFunctions = new Object();
myFunctions.func = function () {

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.

  • 1
Reply Report

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions