5

I'm reading some books on PHP (specifically "PHP and MySQL Web Development" by Welling and Thomson) and I'm also a fresh undergrad. I was a bit curious why the author decided to choose two different ways to terminate the execution of a function, e.g.

if (!$result) {
    throw new Exception('Password could not be changed.');
} else {
    return true;
}

For me, this seems a bit inconsistent and it would make more sense to return false and have the caller check the callee's return value and deal with it. Is it common for PHP code to be like this? Is this the type of style expected when using exceptions?

2
  • 1
    These two, Welling and Thomson are famous for they have no clue. Using exceptions to validate user input is a risky game. I'd limited it's use to system errors only Commented Jun 26, 2010 at 3:50
  • I thought so :) To be honest, it seems like using Exceptions is a bit heavy handed for things like this. Commented Jun 26, 2010 at 4:01

4 Answers 4

4

Yes, I agree it doesn't make much sense. Either signal error conditions through the return value or with an exception. If the return value is always true (on error an exception is raised), you might as well not return anything (which in PHP is equivalent to returning NULL).

Sign up to request clarification or add additional context in comments.

Comments

2

It seems purely stylistic. I agree with you that returning true or false would be more consistent, and the calling code should throw an exception if false is unexpected or unacceptable.

I don't think the PHP community has fully worked out conventions for behavior such as this, so I wouldn't say it is either common or uncommon. There are a hundred different ways that systems like that are designed in PHP.

Comments

1

Most of the time when you see PHP code it will be all about the person who coded it and their particular style.

I don't see what is inconsistent about the code fragment. Exceptions are an appropriate way to deal with errors in most languages. He could omit the return statement but I know many people feel like a function should always return a value.

2 Comments

I feel that if this function were more consistent, then the return values should reflect that. The same way that strcmp() returns < 0, 0, or > 0, I feel like this would make more sense with consistent types for the return values. To be honest, we never learned much about exceptions and exception handling in college, so I'm not sure if this type of coding style is what I should expect in commercial code.
What we are looking at is just a code fragment. It is very hard to judge how to handle the flow of execution. Personally, I wouldn't throw an exception in an if else statement. I don't feel like wrapping the execution code in brackets is going to help it. In my opinion, as far as error handling, exceptions are the way to go because of try..catch blocks. It gives you an opportunity to correct or surpress error before halting execution.
1

Thrown exceptions in PHP terminate the script (or called function) if not handled and do not return anything: see here. On the other hand, a return statement will return a value. So it is not a question of convention or coding style to use such example as is used in the question - The example does not fit the purpose of exception handling (That is; to prevent the program from crashing even after encountering an exception).

That is the reason why PHP provides you with a global Exception class. Good programming practice is:

  1. Always handle exceptions in the catch block of a try-catch statement.
  2. If you are not interested in what caused the exception, but you want your program/script to continue execution, catch the global Exception class in the catch block and handle it.
  3. If you are interested in the exception and know likely causes of such exception, create a class that implements the Throwable interface or inherits the global Exception class. Then catch such exception object in your program and handle them.
  4. Finally, if you want your script (or the called function) to terminate on exception, it is still ideal to throw such exception in a catch block after catching a more general exception object like that of the global Exception class. The caught exception can then be handled by the calling program or terminate the script if not handled.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.