Which would you recommend?
-
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); } } -
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?
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,
would be enough
Exceptions could be useful to handle errors, to know if we had any errors occurred.
A simple example: