Edit report at https://bugs.php.net/bug.php?id=32100&edit=1

 ID:                 32100
 Comment by:         frederic dot hardy at mageekbox dot net
 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:

I'm not sure that this place is the right place to discuss about that.
Since the last year, PHP has a process to discuss technical point, aka RFC 
(https://wiki.php.net/rfc).
So, if "finally" must be included in PHP, just write the relative RFC and 
discuss 
it on internals.
Sure that time has changed, because PHP's users are more power now than in the 
past !


Previous Comments:
------------------------------------------------------------------------
[2011-12-08 17:40:44] antoninweb at gmail dot com

I don't understand how this is not included when PHP supports try...catch. It 
just 
doesn't makes sense and it's annoying because you have to find ways around it 
contantly.

------------------------------------------------------------------------
[2011-12-06 05:50:29] ben at last dot fm

"finally" would be a majorly beneficial addition to the language. It's 
something 
we yearn for here at last.fm.

------------------------------------------------------------------------
[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.

------------------------------------------------------------------------


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

Reply via email to