简体   繁体   English

PHP:die()必须死吗?

[英]PHP: Does die() must die?

Is it considered bad practice to let die() live in a production enviroment? 让die()生活在生产环境中被认为是不好的做法吗? 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. 刚刚读过这篇文章http://www.phpfreaks.com/blog/or-die-must-die ,作者对在生产环境中使用这类东西的人进行了抨击。 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? 你怎么编码?

die() is a very rough statement... Useful (quite) in developer stage, i found it wrong in production stage. die()是一个非常粗略的陈述......在开发阶段很有用(相当),我在生产阶段发现它是错误的

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"! 您应该分析,监控和记录致命错误,并显示足够的消息,例如“无法连接到服务器,请在几分钟内尝试或写信给admin@yourhost.com以通知问题”!

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

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. 如果您正在谈论网站,我会发送HTTP status 500作为回复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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