NYCPHP Meetup

NYPHP.org

[nycphp-talk] "Finally" missing in PHP...

Michael B Allen ioplex at gmail.com
Tue Aug 5 16:19:44 EDT 2008


The most common scenario for try / finally is to release some
resources regardless of whether or not an exception occurred.

For example:

  try {
      $r = new Something();
      $r->doStuff();
  } finally {
      $r->cleanup();
  }

So $r->cleanup() will run regardless of whether or not the try block
code throws an exception.

The "ugly hack" cited catches the exception. You could re-throw it but
that may not be the desired behavior. You might want to catch one type
of exception and yet re-throw another. That's more logic you have to
consider. With a finally clause it's very clear that that code is to
be executed regardless of what happens (sans program exit).

A finally clause would be a good addition to PHP.

Mike

On Tue, Aug 5, 2008 at 11:18 AM, Guilherme Blanco
<guilhermeblanco at gmail.com> wrote:
> exactly =)
>
> On Tue, Aug 5, 2008 at 12:14 PM, Justin Dearing <zippy1981 at gmail.com> wrote:
>> So your saying finally could be implemented as
>>
>> if($e != null){
>>   //finally stuff
>> }
>>
>>
>> On Tue, Aug 5, 2008 at 11:09 AM, Guilherme Blanco
>> <guilhermeblanco at gmail.com> wrote:
>>> You can do an ugly hack.... supported:
>>>
>>>
>>> try {
>>>  // ...
>>> } catch (Exception $e) {
>>>  // ...
>>> }
>>>
>>> // finally... you can use $e here, you know?!!?!? =)

-- 
Michael B Allen
PHP Active Directory SPNEGO SSO
http://www.ioplex.com/



More information about the talk mailing list