Page 1 of 1

trigger_error from libraries in PHP 4

Posted: Wed Feb 07, 2007 10:28 am
by xx9
Hi. For those of us still supporting PHP 4, I wonder if you think it is OK to use trigger_error from library code?
For the most part I've usually returned simple error classes from library classes/functions. I've actually never really liked the trigger_error way of doing things in PHP but lately I've been using it more. But I wonder if it's a bad thing to do this from library code because it takes control out of the hands of the user of the library.
And if it is ok to use trigger_error from libraries what levels are OK to use? Would you want to trigger an E_USER_ERROR (fatal) from a library? I would think not but maybe you can give me a reason why it's OK.

Hope to hear your thoughts.

Posted: Wed Feb 07, 2007 1:23 pm
by feyd
For code that you would normally throw an exception for, trigger_error() is well suited to fill that space in PHP 4.

Posted: Thu Feb 08, 2007 3:49 am
by Maugrim_The_Reaper
Errors, unlike Exceptions, should be triggered when something has "gone wrong". If a user passes an invalid parameter, for example, and the library just cannot do anything with it or return a valid value, then it's better to trigger an error and let the user know it's their code (not yours) at fault. It benefits everyone in the end by reducing user level code which assumes your library is working correctly even though it's not.

Posted: Thu Feb 08, 2007 8:58 pm
by Ambush Commander
I tend to use E_USER_ERROR liberally, but only when it would be immediately obvious what's causing the problem (i.e. code that would be explicitly called).

Not sure if it's a good idea: SimpleTest swallows them anyway.