Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP: Does die() must die?

Tags:

php

mysql

Is it considered bad practice to let die() live 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?

like image 747
AleGore Avatar asked Mar 01 '10 09:03

AleGore


2 Answers

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);
     }
}
like image 91
user187291 Avatar answered Nov 04 '22 07:11

user187291


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 [email protected] to notify the problem"!

like image 33
Enrico Carlesso Avatar answered Nov 04 '22 06:11

Enrico Carlesso