php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #60897 Advocating trigger_error instead of exit/die()
Submitted: 2012-01-26 23:36 UTC Modified: 2013-10-06 07:42 UTC
From: luiheidsgoeroe at hotmail dot com Assigned: krakjoe (profile)
Status: Closed Package: Documentation problem
PHP Version: Irrelevant OS: Irrelevant
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: luiheidsgoeroe at hotmail dot com
New email:
PHP Version: OS:

 

 [2012-01-26 23:36 UTC] luiheidsgoeroe at hotmail dot com
Description:
------------
Although examples in the documentation are of course only meant as illustrations, we all know that starting developers copy/paste huge chunks of code and then fiddle with the details to get things working. Usually, this turns out alright, but the use of exit(<error message>) and/or die(<error message>) on any error (mysql_connect comes to mind) is widespread in the documentation. This is terrible to debug later, does not leave a message in the error log, and in 3rd party included libraries it is just plain evil.

I'd like to advocate:
1) Adding a note to exit()/die() that trigger_error('some message', E_USER_ERROR) would be preferable in production (easily done if people agree with this).
2) Modifying most if not all examples in the documentation that use die() to that trigger_error() (a lot of work).

It is of course debatable whether this should happen, as trigger_error() with display_errors on differs with die() in also showing path & linenumber, which is not information visitors should normally get, but then again, neither should display_errors be on. Also an issue could be it clutters the examples somewhat with non-relevant statements, but it is not terribly more verbose then the die()'s that are there now.

In my opinion, it would advocate good practices in coding and relieve a lot of irritation and obscurity in debugging code from new coders and monitoring issues.

Should this be accepted as indeed the way to go, I would be perfectly willing to comb through the documentation to alter die()/exit() statements to this format and submit a patch, but - as it will take a while - not until there is a definite decision that this is what desirable, otherwise I'd be wasting my time.


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-07-16 15:41 UTC] notdefix at hotmail dot com
The use of trigger_error('Error string', E_USER_ERROR); is far better than die();/exit();

However, in many cases, 'throw new Exception()' might be a better solution. I think it is definately worth improving the examples, be it with exceptions or triggered errors, both are better practice than die/exit.
 [2013-10-06 07:42 UTC] krakjoe@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: krakjoe
 [2013-10-06 07:42 UTC] krakjoe@php.net
As you have correctly observed, documentation is not a work of best use code, they are illustrative examples.

That's really the end of it; it is impractical to expect every example in the manual to be the best possible version of the code that it is; not least of all, there are differing opinions on as to what would constitute the best possible version.

Thanks for your input, but closing the bug, there really isn't a need to change.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC