Edit report at https://bugs.php.net/bug.php?id=32100&edit=1
ID: 32100
Comment by: chiestand at salk dot edu
Reported by: ceefour at gauldong dot net
Summary: Request 'finally' support for exceptions
Status: Closed
Type: Feature/Change Request
Package: Feature/Change Request
Operating System: *
PHP Version: 5.*
Block user comment: N
Private report: N
New Comment:
First, thank you everyone who has contributed to this bug report thread. Your
insights have been incredibly useful.
I too vote for inclusion of "finally" into PHP. In my own particular situation
I was
able to solve my problem using Stroustrup's RAII pattern (thank you btsai). But
I can
imagine that in some cases creating a class for every resource used might be
inconvenient.
I think ceefour really summed it up nicely back in 2005 with even more-ancient
wisdom: "Be conservative with what you emit and be liberal with what you
accept".
Provide the tool, and let the coder decide what pattern to use.
Previous Comments:
------------------------------------------------------------------------
[2011-09-07 14:49:08] viktor at zuiderkwast dot se
The same problem exists in C++, which also lacks the finally clause. The
standard way to solve the resource allocation problem in C++ is instead by
using the RAII design pattern.
As noted in Wikipedia on RAII, "In this language, the only code that can be
guaranteed to be executed after an exception is thrown are the destructors of
objects residing on the stack.". The same is valid for PHP (although the
objects are reference-counted instead of being allocated on the stack). The
destructor is guarranteed to be called as soon as all references to the object
run out of scope, so the RAII is effectively usable in PHP.
So, the code you would put in the finally clause in Java etc has to be put in a
destructor of some object instead. Then, when a return or a or an uncatched
exception occurs inside a try block, and there are no other references to the
object, the destructor will be called at this point to free the resources.
In my optinion, the finally clause is a more elegant solution, although it
might be *too sophisticated* for PHP...
------------------------------------------------------------------------
[2011-07-25 03:27:14] ninzya at inbox dot lv
++finally
------------------------------------------------------------------------
[2011-07-22 07:53:27] dsberliner at gmail dot com
++ for finally in exception handling. Please reconsider.
------------------------------------------------------------------------
[2011-05-30 02:53:58] bat at flurf dot net
Here's an idea! Find all the people who think "finally" isn't needed in PHP.
Invite them to go back to programming Visual Basic, because they're ignorant.
The rest can work on implementing it. Easy!
------------------------------------------------------------------------
[2011-05-05 11:52:26] ealexs at gmail dot com
PHP++ for finally in PHP ;)
my code:
disableSIPTrunk (10 lines of code)
try
{
// do some stuff
}
finally
{
enableSIPTrunk (10 lines of code)
}
// saves duplicate code and it's very elegant !
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=32100
--
Edit this bug report at https://bugs.php.net/bug.php?id=32100&edit=1