Heavily used in PHP
die
Or
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