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

 ID:                 32100
 Comment by:         andrew dot feller at gmail dot com
 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:

The demand for "finally" is a symptom of PHP not officially and explicitly 
addressing supported solutions to managing resources.  I cannot find anything 
within PHP documentation to address this:

http://www.php.net/manual/en/language.oop5.decon.php
http://www.php.net/manual/en/faq.misc.php

So I recommend to move beyond inclusion of finalizers and start with educating 
constituents because there is an opportunity to resolve this and hopefully 
improve quality of work done by developers


Previous Comments:
------------------------------------------------------------------------
[2012-01-02 12:02:49] frederic dot hardy at mageekbox dot net

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 !

------------------------------------------------------------------------
[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();
  }
}

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


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