This article brings the content is about PHP new features: The use of the Finally keyword, there is a certain reference value, the need for friends can refer to, I hope to help you.
Recently, I submitted to the finally Rfc:supports finally keyword has been submitted to the PHP backbone, today to introduce you the background of this new feature, and how to use.
The demand for this feature was first proposed in 2005: FR #32100, but no one has ever realized it. Last month, someone came up, I was holding a try attitude to achieve a bit, because someone told me, has not been achieved, one reason is because it is difficult to achieve (perhaps for a yard, like to challenge the problem, is nature, hehe).
For PHP now, if we need to do something about the exception that we can't handle at the moment, we'll write a code like this:
function Anonymous () {try { function_may_throw_exception (); } catch (Exception e) { Clearup (); throw e; } Clearup (); }
We see that we need to explicitly write two Clearup (). Then finally we can solve the problem.
Finally is not the original PHP, C #, Javascript, Java. And so on other languages, PHP's finally is similar to other languages.
For finally, a more confusing place to return in the finally, because finally must be guaranteed to be executed, so if we return in the try, finally will be called, So what if finally also return? What is the value of the last return? In PHP, if you return in Finally, the original return value will be overwritten.