What is the correct way to handle errors in PHP 4?

Over at comp.lang.php I asked how best to handle errors in PHP 4:

I’m working in PHP 4. Even if a parameter is mandatory, I nearly always give it a default value of “false”. Then I print an error message if someone who is calling my function forgot the mandatory parameter:

function outputHtmlToShowFile($fileName=false) {
if ($fileName) {
// code goes here
} else {
echo “In outputHtmlToShowAFile() the code expected the first
parameter to be a file name, but instead the value was false”;
}
}

How do others handle default parameter values?

I’ve been assuming that relying on the PHP parser to write my error messages for me is unprofessional. I was wondering if other programmers felt that way. I got mixed reactions from others. (I unfortunately used the phrase “professional PHP programmer”, and every respondant made fun of me for that. I apologize for using the phrase)

Iván Sánchez Ortega wrote:

[Catching ever error this way ] is over-engineered and unneccesary.

If “someone” forgets to pass you a parameter, it is because:

- They haven’t looked at your function definition (they are bad programmers)
- You haven’t specified that they have to pass you a parameter (*you* are a
bad programmer)

So:
- Document your code (use doxygen/phpdoc/whatever)
- Tell other programmers you work with how your code works, and what they
can expect from it.

The “professional” way to go is to write specs on paper, then have everyone
read them, then assume everybody will be following them.

Ben Conley had the most interesting response:

My post got longer and longer as my thoughts came together on this subject. The gist of my claim is that you shouldn’t build something as FRAGILE as a function that breaks if there are argument variations, when you can simply build a more robust function in the first place. The final answer? Pass in a structure rather than require ongoing maintenance of all references to a function so as to avoid an error you never should have gotten in the first place.

There are two options here. You can create your function with a static set of arguments that fails when it is called with missing parameters, then counter with the retort that the dude who called it was a “bad programmer.” This is totally useless when your production sites are throwing unnecessary errors. The second option is to pass in a structure and simply check for various optional arguments.

Any “professional” programmer who relies on the vast repository of *documentation* present in most projects in industry is either an idealistic novice, or EXTREMELY fortunate for the places they’ve worked.

I have fantasies that someday I will collect all the good ideas I’ve heard and combine them into the world’s most amazing framework. I admire the way every method in JQuery always returns the object that it is acting upon, and therefore you can chain method calls together. That is an idea I’d like to see in a PHP framework. I love the way all parameters passed into a method in Ruby are automatically what (in PHP terms) amounts to values in an associative array. I’d love to see something like that in a PHP framework. Conley is suggesting something even better than passing in an associative array (which has the weakness that it is a primative), he’s suggesting that we always pass in an object with a particular structure. (Or perhaps there is a base class that gives certain properties to all classes in the system.)

However, Gosha Bine summed up a thought I’ve been having a lot for the last year:

You do test your software before it goes production, don’t you?

That’s exactly right. All that is needed in a system, in terms of error checking, is tests. If you have tests, you don’t really need error checking. And if you find yourself still needing error checking, it probably means you have not yet written enough tests.

Leave a Reply

You must be logged in to post a comment.