views:

90

answers:

4

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?

+1  A: 

3.Use exceptions.

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

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.

Wrikken
A: 

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
}
alex
Are you saying that I should use exceptions in this specific example? Or across the board?
letseatfood
Well if you are using PHP5 then I would go with exceptions.
alex
@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.
Col. Shrapnel
+1  A: 

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();
Col. Shrapnel
@Col. Shrapnel 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.
letseatfood