[ Content | View menu ]

PHP Insanity VII

Mark Mzyk | April 14, 2008

I discovered an interesting bit of insanity in PHP while at work the other day.

Define a function in PHP and make sure it has at least one argument, but that argument should not have a default.

So the function should be something like:

public function foo( $bar )

Not:

public function foo( $bar=fubar )

Now call your function, but leave out the argument.

$foo( );

PHP will only throw a warning about the missing argument.

What PHP should do is throw a fatal error.

If your code somehow manages to run to completion in that state, there is no telling what the potential consequences to your application might be.

Now, you could claim that if the argument is left out that your code should eventually encounter an error and therefore you discover your mistake.  But I shouldn’t have to rely on that bit of happenstance.  Clearly when writing the code I intended for the argument to be there, so when it is left out, PHP should convulse violently, like it’s having withdrawal from crack.

Throwing a fatal is a prevention mechanism that stops me from making an error that humans are prone to make all the time.  We often forget the small things, and in this case PHP should let me know and let me know forcefully.  Not with a puny warning I’m likely to miss.

Otherwise, I might end up losing my sanity.

Filed in: Languages.