trigger_error from libraries in PHP 4

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
xx9
Forum Newbie
Posts: 3
Joined: Fri Feb 02, 2007 8:30 pm

trigger_error from libraries in PHP 4

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

For code that you would normally throw an exception for, trigger_error() is well suited to fill that space in PHP 4.
User avatar
Maugrim_The_Reaper
DevNet Master
Posts: 2704
Joined: Tue Nov 02, 2004 5:43 am
Location: Ireland

Post 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.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post 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.
Post Reply