Returning status code where one of many errors could have occured

Posted by yttriuszzerbus on Programmers See other posts from Programmers or by yttriuszzerbus
Published on 2012-12-14T21:27:26Z Indexed on 2012/12/14 23:21 UTC
Read the original article Hit count: 179

Filed under:
|

I'm developing a PHP login component which includes functions to manipulate the User object, such as $User->changePassword(string $old, string $new) What I need some advice with is how to return a status, as the function can either succeed (no further information needs to be given) or fail (and the calling code needs to know why, for example incorrect password, database problem etc.)

I've come up with the following ideas:

  • Unix-style: return 0 on success, another code on failure. This doesn't seem particularly common in PHP and the language's type-coercion messes with this (a function returning FALSE on success?) This seems to be the best I can think of.
  • Throw an exception on error. PHP's exception support is limited, and this will make life harder for anybody trying to use the functions.
  • Return an array, containing a boolean for "success" or not, and a string or an int for "failure status" if applicable.

None of these seem particularly appealing, does anyone have any advice or better ideas?

© Programmers or respective owner

Related posts about functions

Related posts about error-handling