Edit report at https://bugs.php.net/bug.php?id=32100&edit=1
ID: 32100
Comment by: ben at last dot fm
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:
"finally" would be a majorly beneficial addition to the language. It's
something
we yearn for here at last.fm.
Previous Comments:
------------------------------------------------------------------------
[2011-12-05 17:55:43] php dot net at kenman dot net
+1
>From Zeev, in the 2000 discussion:
> try..finally doesn't make much sense in the context of PHP [...] Nobody has
> ever
asked for this in the past either.
Those days are long past, please take this bug report's comments as a sign that
this *does* now make sense for PHP.
------------------------------------------------------------------------
[2011-12-05 15:53:28] topaz dot php dot bugs at lt3 dot us
Ugly workaround hack time!
(This is not a substitute for a real language feature!)
Mix and match with try/catch blocks as necessary.
<?php
// Usage examples
// With no lambda functions <5.3, we do this instead :(
function example_finally()
{
print "Finally was called!\n";
}
function example1()
{
$finally = new Finally("example_finally");
print "Not throwing exception.\n";
$finally->invoke();
print "Example 1 ended normally.\n";
}
function example2()
{
$finally = new Finally("example_finally");
print "Throwing exception!\n";
throw new Exception("Something exceptional happened!");
$finally->invoke();
print "Example 2 ended normally.\n";
}
// Test harness
print "Example 1...\n";
try
{
example1();
}
catch (Exception $e)
{
print "Example 1 threw an exception!\n";
}
print "\nExample 2...\n";
try
{
example2();
}
catch (Exception $e)
{
print "Example 2 threw an exception!\n";
}
// Implementation of the Finally class
class Finally
{
private $_callback = NULL;
private $_args = array();
function __construct($callback, array $args = array())
{
if (!is_callable($callback))
{
throw new Exception("Callback was not callable!");
}
$this->_callback = $callback;
$this->_args = $args;
}
public function invoke()
{
if ($this->_callback)
{
call_user_func_array($this->_callback, $args);
$this->_callback = NULL;
$this->_args = array();
}
}
function __destruct()
{
$this->invoke();
}
}
------------------------------------------------------------------------
[2011-11-18 00:25:23] chiestand at salk dot edu
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.
------------------------------------------------------------------------
[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
------------------------------------------------------------------------
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