让die()生活在生产环境中被认为是不好的做法吗?刚刚读过这篇文章http://www.phpfreaks.com/blog/or-die-must-die,作者对在生产环境中使用这类东西的人进行了抨击.所以我不应该这样编码:
$connection = mysql_connect($db_host, $db_username, $db_password);
if (!$connection){
die ("Could not connect to the database.");
}
Run Code Online (Sandbox Code Playgroud)
你怎么编码?
你不会每次犯错都会死,对吗?为什么你的应用程序应该这样做?
正确的方法是拦截错误并以上下文相关的方式处理它们,例如
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);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
399 次 |
| 最近记录: |