1

Which would you recommend?

  1. Return an error code, such as E_USER_ERROR from a function, and determine proper message higher up:

    function currentScriptFilename()
    {
      if(!isset($_SERVER['SCRIPT_FILENAME']))
      {
        //This?
        return E_USER_ERROR;
      }
      else
      {
        $url = $_SERVER['SCRIPT_FILENAME'];
        $exploded = explode('/', $url);
        return end($exploded);
      }
    }
    
  2. Execute trigger_error() from the function, with a specific error message:

    function currentScriptFilename()
    {
      if(!isset($_SERVER['SCRIPT_FILENAME']))
      {
        //Or this?
        trigger_error('$_SERVER[\'SCRIPT_FILENAME\'] is not set.', E_USER_ERROR);
      }
      else
      {
        $url = $_SERVER['SCRIPT_FILENAME'];
        $exploded = explode('/', $url);
        return end($exploded);
      }
    }
    

I am not sure if I will regret having put a bunch of error messages in my functions further down the line, since I would like to use them for other projects.

Or, would you recommend something totally different?

4 Answers 4

1

Do not mix the matters.
Error notification and error handling are different tasks.

You have to use both methods simultaneously.
If you think that $_SERVER['SCRIPT_FILENAME'] availability is worth an error message, you can use trigger error. However PHP itself will throw a notice if you won't check it.

If you want to handle this error, just check this function's return value.
But I would not create a special function for this task.

So,

if (!$filename = basename($_SERVER['SCRIPT_FILENAME']) {
  //  do whatever you want to handle this error.
}

would be enough

Exceptions could be useful to handle errors, to know if we had any errors occurred.

A simple example:

try {
  $filename = basename($_SERVER['SCRIPT_FILENAME']) 
  if (!$filename) throw new Exception("no filename");

  $data = get_some_data_from_db() or throw new Exception("no data");

  $template = new Template();
 //Exception could be thrown inside of Template class as well.
}
catch (Exception $e) {
  //if we had any errors
  show_error_page();
}
$template->show();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for pointing out basename! Your example of throwing exceptions is very helpful. I have been spending the last week reading articles and trying out handling errors and exceptions in my projects.
Adding to your comment; you usually trigger/raise/throw errors, not return them. However, you may return success states (true/false, file/null, resource/null, object/null ...).
1

3.Use exceptions.

4 Comments

Would you mind explaining why you would use exceptions in this case?
They provide an unambiguous indication that you've hit an exceptional situation. They can't be implicitly ignored, only handled somehow.
well the question remains the same - where to throw an exception - inside or outside :)
@Col. Shrapnel: Inside the function.
1

If this is the route you are going, I'd rather recommend throwing Exceptions rather then returing an E_ERROR (E_USER_ERROR should be used), as this is just an integer, and possibly a totally valid return for your function.

Advantages:
- Throwing of an Exception cannot be interpreted as anything else then an error by mistake.
- You keep the possibility to add a descriptive error message, even though you don't handle the error at that point/
- You keep a backtrace in your Exception.
- You can catch specific exceptions at specific points, making the decision where in your project a specific type of error should be handled a lot easier.

Comments

0

If not using exceptions which you should be, use trigger_error().

If it is an error you'd like to deal with, try returning FALSE like a lot of the in built functions do.

If you do use exceptions, catch them like this

try {
    whatever()
} catch (Exception $e) {
    // handle however
}

3 Comments

Are you saying that I should use exceptions in this specific example? Or across the board?
Well if you are using PHP5 then I would go with exceptions.
@letseatfood across the board but not in this simple case. Exceptions mostly used to handle errors, to control the program flow, rather than for the the error notification.

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.