php function junction

Working well with php functions

Most of the time its very tempting to open up your text editor and start writing code to scratch some itch. Smart programmers know to use functions to wrap up repetitive tasks and make life easier on themselves. The following piece is meant to give your functions a little extra edge both during development and months later when you come back to fix or improve your php script.

Give your functions some class

Sooner or later, you’ll build up libraries of functions that you use quite often. Each library could be its own separate file that you include() or require() at the beginning of the script. For example, when you are working with webpages you could have a library that makes outputting different kinds of lists (as list items, radio buttons, checkboxes, and so on). A second library might clean up strings and display them to the output buffer. These libraries might look a little like this:

--Lists.php--
function display() {
/* code to display an array as a series of <li> tags */
}

function radio() {
/* code to display array as a series of radio buttons */
}

function select() {
/* code to display an array as a series of select options */
}

--Output.php--
function display() {
/* code to run a string through trim, strip_tags, html_entities
and display it. */
}

function USphone() {
/* function to run a numeric string and output as a us phone in
format (999)888-7777
*/
}

function radio() {
/* function to run a string and output the first four letters
in all caps */
}

Clearly, the above function signatures are for example only but there are some apparent limitations to organizing function libraries this way. Since all functions exist in the global namespace, you have to keep track of what function names have already been used. This may be managable for a couple of include files but once you are working with more than a half a dozen or so it becomes tedious to inspect them all to make sure your new function name isn’t taken. If you include both files in one script, PHP will produce an error when you try to define an already exisiting function.Finally, when you are maintaining code, it can be a pain to have to figure out which radio() or display() function you are calling in a script.
A simple solution to the above limitations is to wrap your function library as a class as shown below.

--Lists.php--
class Lists {
function display() {
/* code to display an array as a series of <br> tags */
}

function radio() {
/* code to display array as a series of radio buttons */
}

function select() {
/* code to display an array as a series of select options */
}
} // end of class Lists
--Output.php--
class Output {
function display() {
/* code to run a string through trim, strip_tags, html_entities
and display it. */
}

function USphone() {
/* function to run a numeric string and output as a us phone in
format (999)888-7777
*/
}

function radio() {
/* function to run a string and output the first four letters
in all caps */
}
} // end of class Output

Each function library is now safely cocooned in its own class namespace. The functions are then called using static calls like so:

// output station's name and phone number
echo Output::radio($letters).'<br>';
echo Output::USphone($phone).'<hr>';
//output radio options of deejay names
echo List::radio( $deejays );

Its a little more typing but now its obvious what class and file the function is in if you ever need to modify it. Also, when writing new functions you don’t have to worry if the name of your function is already in use.

Other function tips

  1. Don’t solve a problem in the middle of outputting a page or tackling another task – make it a function. Even if its only used on that page.
  2. If you are copying and pasting lines from one place to another and changing some variables around, its should be a function.
  3. After writing a function, see if you can abstract it a little more. Chances are you wil have to solve a similar problem later.
  4. Instead of echoing any output, use php’s output buffering functions to capture the output and return it as a string.
  5. Don’t rely on globals in your function. If your function needs it, it should be passed to it.

Other references