Jump to content

PHP gets 'finally' support


scootstah

Recommended Posts

https://wiki.php.net/rfc/finally

 

Neat.

 

Some examples:

<?php
try {
    return 2;
} finally {
    echo "this will be called\n";
}
//this will never be called
echo "you can not see me";

// output - this will be called
// return int(2)

 

<?php
function foo ($a) {
    try {
        echo "1";
        try {
            echo "2";
            throw new Exception("ex");
        } catch (Exception $e) {
            echo "3";
        } finally {
            echo "4";
            throw new Exception("ex");
        }
    } catch (Exception $e) {
        echo "3";
    } finally {
        echo "2";
    }
    return 1;
}

var_dump(foo("para")); 

// output - 123432int(1)

Link to comment
Share on other sites

Wow, what an awkward subject title when context isn't known :D

My brain "fixed" it to "PHP finally gets support" until I read it. :-P

 

I am confused about the part they said is confusing. Why wouldn't code that's completely outside of the try/finally block not run?

Link to comment
Share on other sites

Wow, what an awkward subject title when context isn't known :D

My brain "fixed" it to "PHP finally gets support" until I read it. :-P

 

I am confused about the part they said is confusing. Why wouldn't code that's completely outside of the try/finally block not run?

Because of the try block returning.

Link to comment
Share on other sites

Wow, what an awkward subject title when context isn't known :D

My brain "fixed" it to "PHP finally gets support" until I read it. :-P

 

I am confused about the part they said is confusing. Why wouldn't code that's completely outside of the try/finally block not run?

 

The return call.

 

SNIPED!

Link to comment
Share on other sites

Hmm.. Now we only need to wait for people to start to abuse this, and "experts" starting to rant about how "PHP sucks" because of "finally" in addition to everything else. :P

But, a bit more seriously, I have to say that PHP have become a lot more powerful lately. Especially in the OOP section, seems to be a quite good development as well. Will be interesting to see what happens when they finally finish PHP 6. :)

Link to comment
Share on other sites

Hmm.. Now we only need to wait for people to start to abuse this, and "experts" starting to rant about how "PHP sucks" because of "finally" in addition to everything else. :P

But, a bit more seriously, I have to say that PHP have become a lot more powerful lately. Especially in the OOP section, seems to be a quite good development as well. Will be interesting to see what happens when they finally finish PHP 6. :)

 

Yeah, but too bad all the awesome new features won't really be mainstream for at least another year. I mean, 5.3 is barely mainstream at this point. Most hosts support it, but it's not always enabled by default.

Link to comment
Share on other sites

Look at the RAII link I posted above.  Finally blocks are usually used as a place to close/dispose of resources (db connections, file handles, etc.).  I'm not sure if PHP usually takes care of those kind of resources at the end of script execution or not (IIRC, db connections are automatically closed, but unsure about files), but in other languages resources aren't generally disposed by garbage collection, so finally blocks give the programmer a tidy place to do it.

Link to comment
Share on other sites

or i guess more accurately... just do whatever i'm going to do after the try...catch, not inside it.  how is doing it within 'finally' different?

 

I was thinking something along the lines of (oversimplified)

 

try {
$template->output('header');
$data_sidebar = $control->getStuff($userData);
$data_content = $contro->getOther($moreData);
$template->buffer('sidebar',$data_sidebar);
$template->buffer('content',$data_content);
$template->flush();
} catch ( Exception $e ) {
$template->clear_buffer();
$template->output('error',$e->getMessage());
} finally {
$template->output('footer');
}

Link to comment
Share on other sites

@xyph:

 

but why can't I just do

 


try {
$template->output('header');
$data_sidebar = $control->getStuff($userData);
$data_content = $contro->getOther($moreData);
$template->buffer('sidebar',$data_sidebar);
$template->buffer('content',$data_content);
$template->flush();
} catch ( Exception $e ) {
$template->clear_buffer();
$template->output('error',$e->getMessage());
}
$template->output('footer');

 

I'm not seeing what the advantage of using finally is, over this?

Link to comment
Share on other sites

Maybe

 

try {
$template->output('header');
$data_sidebar = $control->getStuff($userData);
$data_content = $contro->getOther($moreData);
$template->buffer('sidebar',$data_sidebar);
$template->buffer('content',$data_content);
$template->flush();
} catch ( Exception $e ) {
$template->clear_buffer();
$template->output('error',$e->getMessage());
die();
} finally {
$template->output('footer');
}

 

?

Link to comment
Share on other sites

Look at the RAII link I posted above.  Finally blocks are usually used as a place to close/dispose of resources (db connections, file handles, etc.).  I'm not sure if PHP usually takes care of those kind of resources at the end of script execution or not (IIRC, db connections are automatically closed, but unsure about files), but in other languages resources aren't generally disposed by garbage collection, so finally blocks give the programmer a tidy place to do it.

 

You can clean up your own mess as you go (for instance close db connections), but php does clean up all of that when the script ends.  But even still, this isn't a "run this before the script ends" thing unless you wrap the entire script in a giant try..catch..finally - something I doubt most people would actually do...

 

 

I'm sure I'm missing something here..I can't see how something so useless/pointless would make it into php ... but.. I just can't think of a good use case, so.../shrug

Link to comment
Share on other sites

I found this link https://bugs.php.net/bug.php?id=32100

 

near as I can figure most "pro" finally people basically say this is unnecessarily duping code:

 

try {
  // allocate resources
} catch  (e) {
  // deallocate resources
} 
// deallocate resources

 

and finally will fix that by doing:

 

try {
  // allocate resources
} catch (e) {
  // anything else, but catch not even needed
} finally {
  // deallocate resources
}

 

What I don't understand is, why can't you just do this?

 


try {
  // allocate resources
} catch (e) {
  // anything else, but catch not even needed
}  
// deallocate resources

Link to comment
Share on other sites

What I don't understand is, why can't you just do this?

 

It's nice to have if you need to do a cleanup, but don't necessarily want to catch exceptions (let them be handled higher up or something).  Sure you can work around it by just catching and re-throwing but it's less than ideal imo.

 

 

try {
   //something making $tmpfile on disk
}
finally {
  unlink($tmpfile);
}

 

is nicer / less bug prone than

 

try {
   //something
   ///....
  unlink($tmpfile);
}
catch (Exception $e){
  unlink($tmpfile);
  throw $e;
}

 

 

I've had on a few instances small bugs due to a change in cleanup code that got made in one spot but not the other.  Being about to keep it in just one area would prevent that.

Link to comment
Share on other sites

If an exception is caught the resources are never deallocated.

 

What I don't understand is, why can't you just do this?

try {
  // allocate resources
} catch (e) {
  // anything else, but catch not even needed
}  
// deallocate resources

Link to comment
Share on other sites

  • 3 weeks later...

Another feature from Java implemented, nice. If PHP keeps doing this they may as well consider re-creating the language based on Java for version 6, but keep their own syntax such as dynamic typing and ->/:: operators. After all, the language was originally built on C, an old-fashioned procedural language that is no longer useful nowadays with OOP as the dominant programming paradigm.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.