05
Why people still use PHP4
... or, the incredible shrinking feature-set
Everybody thinks PHP5 is so great, I hear it all the time. People say, "We should use PHP5, it's so great", "It has much better support for OOP", "You can do so much more with 5 than with 4", etc. etc. You get my point.
The problem is, every new feature of PHP5 has some crippling bug that causes it to be unusable, or at least not usable in a way that makes your code better or easier to understand.
Today, we will examine exceptions in PHP5. Exceptions used to be the only feature of 5 that I would concede and say it is nicer than 4. Because, there is no amount of code magic that can jump the flow of code like a try/catch block. In case you don't know, the real benefit of a try catch block is that you can group statements together as a logical unit, if one fails, they all fail (rather, the rest of them fail with an opportunity to clean up).
<?php
try {
$zone = new DateTimeZone('qqq');
} catch (Exception $e) {
echo "please select a different time zone.";
}
Looks innocent enough, right? But watch how this useful tool becomes completely worthless. Most, if not all, PHP systems need to use a custom error handler. You really need to track all the warnings and errors that happen on a live system in order to spot attempted break-ins and bugs. When you set a custom error handler PHP sends all exceptions to your custom error handler as an E_WARNING. It is indistinguishable from a regular warning. In your error handler, if you return false, the error will be handled as normal. The problem is, you can't tell when you should return false, so as to continue regular exception handling, and when you shouldn't in order to swallow up the warning. Therefore, your catch block will never get executed when you're using a custom error handler.
The only real way to solve this issues is to unset your custom error handler before every try/catch block that would trigger a core PHP exception.
<?php
restore_error_handler();
try {
$zone = new DateTimeZone('qqq');
} catch (Exception $e) {
echo "please select a different time zone.";
}
set_error_handler('my_handler');
Does that look more powerful to you? Does that look more elegant? Where's the power, I ask.



