Improved Error Messages in PHP 5
October 7th, 2007Sometimes its the little things that make a difference. If you run the this test program in PHP 4 (tested on 4.4.7):
< ?php function test($arg) { echo "talk like a pirate."; } test(); ?>
You get the following message:
Warning: Missing argument 1 for test() in /usr/bin/- on line 2
The error message here is reported at the position of the definition of the function, but really the error was in how the function was called. The required parameter to test was not passed. This error can be annoying, forcing you to consult a stack trace to find the actual error location. Something some beginners may not know how to do.
However, if you run the same message in PHP 5 (tested on 5.2.2):
Warning: Missing argument 1 for test(), called in /Users/jeff/- on line 3 and defined in /Users/jeff/- on line 2
Sweet improvement!
One more reason to ditch PHP 4 and go php 5.
October 7th, 2007 at 11:51 pm
Hello,
I consider that it is better to convert all warnings into exceptions as follows:
http://www.alexatnet.com/node/23
October 8th, 2007 at 4:50 am
Yes, and there are more of them, especially when working with objects. And xdebug2 makes this even better as it displays a full stracktrace.
October 8th, 2007 at 9:01 am
Sascha, XDebug is really handy.
Alex,
I don’t recommend converting php errors or warnings to exceptions. If you are integrating code from multiple parties, or running mixed code that targets php 4, it won’t be aware of the exceptions. That code won’t expect the exceptions and might not work correctly for common error conditions. I talk about this issue in my December 2006 column on exceptions in php | architect.
October 9th, 2007 at 2:37 am
I requested this improvement a couple of years ago through the PHP bug database after becoming frustrated with the responses I got when using type hints. You made my day with this blog post.
October 24th, 2007 at 12:42 pm
I have used php5 for 2 yeaers
I’m happy.
November 21st, 2007 at 6:03 pm
I love XDebug. Nothing better than it.
February 12th, 2008 at 11:21 pm
Nobody should be writing in php4 anymore. If you have legacy apps that have OOP in them, OK, I weep for you. But php5 has been mature enough for years and plenty of good webhosts support it 100%. It’s time to put php4 to bed and let the php4 centric anti-php arguments fade off into irrelevance.
March 31st, 2008 at 11:34 am
As of PHP 5 new error reporting constant E_STRICT was introduced with value 2048. It enables run-time PHP suggestions on your code interoperability and forward compatibility, that will help you to keep latest and greatest suggested method of coding. E.g. STRICT message will warn you on using deprecated functions.
E_ALL does not include E_STRICT so it’s not enabled by default
To enable full error reporting (recommended for development boxes) use:
use error_reporting(E_ALL | E_STRICT);
or in php.ini:
error_reporting = E_ALL | E_STRICT
May 7th, 2008 at 8:14 am
I’m just wondering. Have you ever tried to use symfony, a framework of php? I am a computer science student and my professor said that it’s good to use that framework in one of the systems that we will be doing. I have googled up blogs about PHP and i was directed to this blog.
June 26th, 2008 at 1:38 pm
This made me switch my local server to PHP 5 (finally).
July 1st, 2008 at 6:29 pm
I’m trying to migrate all clients onto PHP5 but it’s actually fairly hard when they have tons of existing code built around PHP4, sure it will mostly work but migrating it all over is not an easy process or quick process and likely a lot of PHP4 code and servers will remain for a fair few years yet.
August 3rd, 2008 at 10:55 am
PHP 5 is a huge step forward compared to PHP 4, that’s for sure. I agree to retry, that no one should be writing PHP 4 anymore. listing all the things why you should switch to PHP5 would probably take too much coffee than human organism could handle, error reporting just being one of them.
I truly agree, that error messaging improvement is a great and necessary update to PHP language.