简体   繁体   English

php OOP异常还是死()?

[英]php OOP Exceptions or die()?

I am developing some project. 我正在开发一些项目。 And I want to control different errors. 我想控制不同的错误。 I know that in all popular frameworks and php projects there are different Exceptions. 我知道在所有流行的框架和php项目中都有不同的异常。 But I think that is not required work. 但我认为这不是必需的工作。 If the error is occured we can make die() with our message. 如果发生错误,我们可以使用我们的消息制作die()。 1. What are the main pluses of Exceptions? 1.例外的主要优点是什么? 2. Can I control my errors with die()? 2.我可以用die()控制我的错误吗?

Thank you. 谢谢。

Well, you could use die() . 好吧,你可以使用die() But that makes all errors fatal. 但这会使所有错误都致命。 Meaning that you cannot try to recover from the error at all. 这意味着您无法尝试从错误中恢复。 In some cases that's fine to do. 在某些情况下,这样做很好。

But in most cases, you may want the ability to "clean up" after the error, or to try another method. 但在大多数情况下,您可能希望能够在错误后“清理”,或尝试其他方法。 This is where exceptions come in handy... They let you chose where and if you want to handle the error. 这是异常派上用场的地方......它们让您选择在哪里以及是否要处理错误。 They let you try to gracefully recover from the errors. 它们让您尝试从错误中优雅地恢复。

For example, let's say you have a method which downloads a file from a remote server: downloadFromRemoteServer($address); 例如,假设您有一个从远程服务器downloadFromRemoteServer($address);文件的方法: downloadFromRemoteServer($address);

If you use die() , if the download fails, the script terminates. 如果使用die() ,则如果下载失败,脚本将终止。 End of story. 故事结局。

But if you use exceptions, you could try another server or even try a different method (HTTP vs FTP, etc): 但是如果你使用异常,你可以尝试另一个服务器,甚至尝试不同的方法(HTTP与FTP等):

try {
    $file = downloadFromRemoteServer('http://example.com/foo');
} catch (DownloadFailedException $e) {
    try {
        $file = downloadFromRemoteServer('http://secondtry.example.com/foo');
    } catch (DownloadFailedException $e2) {
        die('Could not download file');
    }
}
return $file;

But remember that Exceptions are useful only for exceptional circumstances. 但请记住,例外仅对特殊情况有用。 They are not meant to be used for any possible error. 它们并不意味着用于任何可能的错误。 For example, if a user doesn't verify their email address correctly, that's not exceptional. 例如,如果用户没有正确验证他们的电子邮件地址,那也不例外。 But if you can't connect to the database server, or have a conflict in the DB, that would be an exception circumstance... 但是如果你无法连接到数据库服务器,或者数据库中存在冲突,那将是一个例外情况......

Alexander, 亚历山大,

die() and Exceptions accomplish different things. die()和Exceptions完成不同的事情。

the "die" language construct just halts the execution of a script and possibly outputs the parameters if it has been called like a function. “die”语言构造只是暂停脚本的执行,如果像函数一样被调用,可能会输出参数。

On the other hand, exceptions are more advanced structures that are used in OOP contexts to give the developer more flexibility as to whether a script needs to be stopped and if so, in what manner, what output to be shown to the user etc. 另一方面,异常是在OOP上下文中使用的更高级的结构,以便为开发人员提供更多的灵活性,以确定是否需要停止脚本,如果需要,以何种方式,向用户显示什么输出等。

Exceptions are a little bit more complex than this so you should perhaps document yourself with some OOP first or for that matter read about zend framework and you'll get a grasp of what Exceptions are. 例外情况比这更复杂,所以你应该首先用一些OOP来记录自己,或者就读取zend框架而言,你将会了解异常是什么。

For simple stuff though, you can always use exit (or die, which is the same thing). 但是对于简单的东西,你总是可以使用exit(或者die,这是相同的东西)。

I hope this helps, Slavic 我希望这有帮助,斯拉夫

1 What are the main pluses of Exceptions? 1例外的主要优点是什么?

The main advantages are: 主要优点是:

  • failing functions don't have to pollute their return with error conditions 失败的函数不必在错误条件下污染它们的返回
  • typed exceptions can be handled at appropriate levels in the code, you decide which portion of the code can handle which errors 可以在代码中的适当级别处理类型化异常, 您可以决定代码的哪个部分可以处理哪些错误
  • you can store a lot more information about the error condition in the exception itself, making handling it, and possibly recovering from it, easier. 您可以在异常本身中存储有关错误条件的更多信息,使其更容易处理,并可能从中恢复。

2 Can I control my errors with die()? 2我可以用die()控制我的错误吗?

I'd hardly call it control, I'd call it giving up on actually handling an error. 我几乎没有把它称为控制,我称之为放弃实际处理错误。 At no point in my projects is a die() actually user-friendly, and all those die('...some error condition...'); 我的项目中没有任何一个是die()实际上是用户友好的,所有这些都die('...some error condition...'); examples of PHP code are IMHO only suited for projects in development. PHP代码的示例是IMHO仅适用于开发中的项目。 In production, you'll want your users to be able to continue their tasks / programs in the easiest way possible, so a 'try again' (if error condition is not likely to be met again), 'sorry that doesn't work' / other kinds of messages, forms / pages are all more desirable then die() . 在制作中,你会希望你的用户能够以最简单的方式继续他们的任务/程序,所以'再试一次'(如果错误条件不太可能再次遇到),'抱歉,这不起作用'/其他类型的消息,表单/页面都比die()更令人满意。

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

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