【发布时间】:2011-10-27 03:26:18
【问题描述】:
关于 PHP 中的错误处理——据我所知有 3 种样式:
-
die()或exit()风格:$con = mysql_connect("localhost","root","password"); if (!$con) { die('Could not connect: ' . mysql_error()); } -
throw Exception风格:if (!function_exists('curl_init')) { throw new Exception('need the CURL PHP extension. Recomplie PHP with curl'); } -
trigger_error()风格:if(!is_array($config) && isset($config)) { trigger_error('Error: config is not an array or is not set', E_USER_ERROR); }
现在,在 PHP 手册中使用了所有三种方法。
我想知道我应该更喜欢哪种风格以及为什么?
这 3 种产品是否可以相互替换,因此可以互换使用?
有点 OT:是我还是大家认为 PHP 错误处理选项只是 太多 到让 php 开发人员感到困惑的程度?
【问题讨论】:
-
这些不是“样式”。它们是不同的语言特征。用于不同的目的。
-
@mario:不同的缩进目的是什么?请赐教:)
-
你提出的问题很好。谢谢提问
标签: error-handling php