scootstah Posted August 13, 2012 Share Posted August 13, 2012 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) Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/ Share on other sites More sharing options...
KevinM1 Posted August 13, 2012 Share Posted August 13, 2012 Finally.... But, yeah, good addition. Could help a little with RAII, although, I'm not sure how much of an issue it is in PHP. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369047 Share on other sites More sharing options...
xyph Posted August 13, 2012 Share Posted August 13, 2012 Wow, what an awkward subject title when context isn't known Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369048 Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 Wow, what an awkward subject title when context isn't known 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? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369050 Share on other sites More sharing options...
Maq Posted August 13, 2012 Share Posted August 13, 2012 Wow, what an awkward subject title when context isn't known 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. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369053 Share on other sites More sharing options...
xyph Posted August 13, 2012 Share Posted August 13, 2012 Wow, what an awkward subject title when context isn't known 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! Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369054 Share on other sites More sharing options...
Jessica Posted August 13, 2012 Share Posted August 13, 2012 AHHH! Duh. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369057 Share on other sites More sharing options...
Christian F. Posted August 13, 2012 Share Posted August 13, 2012 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369161 Share on other sites More sharing options...
scootstah Posted August 13, 2012 Author Share Posted August 13, 2012 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. 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. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369163 Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 Well... That bit is just up to us PHP devs to apply the correct amount of pressure, no? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369187 Share on other sites More sharing options...
.josh Posted August 14, 2012 Share Posted August 14, 2012 okay i readily admit i'm not the brightest crayon in the box...can someone please explain why this is cool or useful? Why wouldn't i just put whatever would go in the 'finally', into the catch instead? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369197 Share on other sites More sharing options...
.josh Posted August 14, 2012 Share Posted August 14, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369199 Share on other sites More sharing options...
Christian F. Posted August 14, 2012 Share Posted August 14, 2012 "Finally" happens regardless of whether or not an exception occurs, so that if you have some process that needs to run in either case you do not have to duplicate the code for it. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369203 Share on other sites More sharing options...
KevinM1 Posted August 14, 2012 Share Posted August 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369205 Share on other sites More sharing options...
xyph Posted August 14, 2012 Share Posted August 14, 2012 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369206 Share on other sites More sharing options...
.josh Posted August 14, 2012 Share Posted August 14, 2012 @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? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369225 Share on other sites More sharing options...
xyph Posted August 14, 2012 Share Posted August 14, 2012 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'); } ? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369229 Share on other sites More sharing options...
.josh Posted August 14, 2012 Share Posted August 14, 2012 okay well, fair enough, you *can* do that...but why? Wouldn't it be more prudent to not have that die (or return etc..) in there? Still not gettin' it :/ Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369231 Share on other sites More sharing options...
.josh Posted August 14, 2012 Share Posted August 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369237 Share on other sites More sharing options...
.josh Posted August 14, 2012 Share Posted August 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369239 Share on other sites More sharing options...
KevinM1 Posted August 14, 2012 Share Posted August 14, 2012 Maybe it's for developers coming from environments where RAII is a concern? It would foster easier language adoption or app porting (think of the relatively common "Translate this Java/.NET script to PHP" questions we get) for those thrust into PHP. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369240 Share on other sites More sharing options...
kicken Posted August 14, 2012 Share Posted August 14, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369250 Share on other sites More sharing options...
Maq Posted August 14, 2012 Share Posted August 14, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1369302 Share on other sites More sharing options...
Hall of Famer Posted August 30, 2012 Share Posted August 30, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1373806 Share on other sites More sharing options...
trq Posted August 30, 2012 Share Posted August 30, 2012 an old-fashioned procedural language that is no longer useful nowadays Are you on crack? Quote Link to comment https://forums.phpfreaks.com/topic/267028-php-gets-finally-support/#findComment-1373812 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.