简体   繁体   中英

Return PHP error constant (such as E_USER_ERROR) from function, or use trigger_error?

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?

3. 使用例外

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.

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();

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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM