Is it considered bad practice to let die() liv开发者_如何学Goe in a production enviroment? Just happened to read this article http://www.phpfreaks.com/blog/or-die-must-die where the author fies upon people who use such a thing in a production enviroment. So shouldn't I code this way:
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database.");
}
How do you code?
You don't die everytime you make a mistake, do you. Why should your application do?
the correct way is to intercept errors and process them in a context-dependent fashion, for example
try {
application goes here
$conn = mysql_connect(...)
if(!$conn)
throw ....
....
} catch(Exception $err) {
if(PRODUCTION) {
log error
say something nice
}
if(DEBUG) {
var_dump($err);
}
}
die()
is a very rough statement... Useful (quite) in developer stage, i found it wrong in production stage.
You should analyze, monitor and log fatal errors, and displaying adequate messages like "It's impossible to connect to the server, please try in few minutes or write to admin@yourhost.com to notify the problem"!
Well in a productive enivornment, you should never expose any errors/information about the system to the outside world.
Important is, to log all errors. If you are talking about a website, I would send an HTTP status 500
as response.
精彩评论