Problem With trigger_error() In PHP4
As SugarCRM developer I have to use PHP although I don’t like it. Today I decided to add a custome error handler to hide actual errors from users. I found a good explanation how to do it but I faced with following problem. To catch critical errors I replaced die()
by trigger_error()
and specify error_type
as E_USER_ERROR
(I didn’t find Perl analog eval
). It works find under PHP 5 but on PHP 4 error messages which pass to trigger_error()
cut up 1024 symbols as it’s described in PHP documentation.
What I don’t understand why trigger_error()
works with different results under PHP 4 and PHP 5? And is there some way to catch external die()
? For example, in Perl I can use eval
:
use SomeModule;
my $o = new SomeModule();
eval {
$o->do_something_with_die();
};
if($@) {
#We catch die()
warn "[Exception] $@!";
}
PHP doesn’t provide such kind functionality. And if I use some external module like PearDatabase.php
I cannot catch die()
if database is not avalilable.
You can’t catch die() in PHP as it just terminates execution of the script. That’s why the better practice in PHP5 is to use exceptions instead. But in PHP4 you can’t do much about it – the only thing you can play with is register_shutdown_function() call though the possibilities here are quite limited.
And what exactly do you mean by “why trigger_error() works with different results under PHP 4 and PHP 5?” If you need longer error messages why don’t just use some kind of external storage? For example you can store your message in $_GLOBALS[‘errmsg’] instead. Or you can pass global var’s name instead of error msg and then use it in error handler like $_GLOBALS[$errmsg]. Quite ugly but should work in both versions and you can give objects and other stuff to your handler this way.
PS: my icq is 69991809, feel free to msg me if you any PHP help 8)
Thanks a lot for you replay.
> And what exactly do you mean by “why trigger_error() works with different results under PHP 4 and PHP 5?â€
Under PHP5 the message is not cut! That’s why I’m a little bit confused. I have installed PHP5 on my computer but on production server we have PHP 4 and there is no way to upgrade it in the nearest future 🙂
Use $_GLOBAL to store error message is not bad idea. I think I’ll do like that.
Thanks for ICQ!
> Under PHP5 the message is not cut!
Expect a lot more of the similar small (and bigger) incompatibilities between versions. Backward compatibility exists but it is surely not the strongest side of PHP.
I understand it but there is not any information about it in PHP documentation.