Is there a downside to using die or exit extensively in PHP?

Source: Internet
Author: User
Heavily used in PHP dieOr exit, are there any other drawbacks in terms of readability?

Reply content:

Is there a lot of use in PHP die or is exit there any other drawbacks in terms of readability?

I think die() / exit() mainly affect the design of the program, that is, the impact of readability and future maintainability. I myself was resolutely resisting the use of no brains die() / exit() .

Before the introduction of the exception mechanism in PHP, die() / exit() is the only way to exit the program in advance, with a well-used. However, with the exception mechanism, you should learn the Java design idea: The business error is returned with the return value, cannot handle the request throws the exception. This design ensures that the caller has enough control in any case to make the whole system robust.

When designing tool classes and tool functions, die() / exit() should be banned as they do not have the right to determine the life and death of the entire program.

When you write a specific business script, die() exit() or use it only if you need to return a status code to the shell or if you are sure the business has ended, it is best to disable it in other cases.

die()And exit() all are expressed here on the natural end, I feel very normal ah, why there is a problem of readability? The idea that extra-long logic nesting and multiplicity include is more likely to cause problems with your so-called readability.

Exit and die are human end scripts run, as the name implies, readability is certainly not a problem.
Performance, the direct end of the script run, the performance is beneficial harmless.
If you have to say the drawbacks, then I think is: should be used to return when the use of exit, the script may exit early, resulting in functional problems. However, I think this is the problem of people writing code, and the grammatical structure is not related.
So, the use of it, rest assured that the use of bold, provided is to let people use.

For the program code writing, I agree with Childe @ Childe die and exit is the program end.

But for the design of functions and libraries, I don't think I should use these two return . Because of a call to a function, the entire program should not be stopped (OMG, this function has a bit too much permission). Whether the function is called correctly or if there is a problem, it should be notified to his callers, and the method is, of course, passed return .

There are no performance problems yet, but if you use the Trigger_error () function instead of die (), your code will have an advantage in handling errors, and it is easier for the client programmer to handle errors.
Here is an article I forwarded, you can see: http://blog.csdn.net/yipiankongbai/article/details/17568223

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.