Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Frank de Jonge:
Finally, file streams, and deferred execution in PHP.
Jun 03, 2016 @ 16:26:52

In a post to his site Frank de Jonge looks at a few different topics around the idea of "cleaning up after yourself" when it comes to the use of finally, file streams and deferred execution.

Cleaning up after yourself can be a tedious task. For example, closing file handlers after using them needs to be done. A programmer's life isn't all about the happy path. When things go pear-shaped you might end up duplicating cleanup code throughout your code. This is horrible, let's explore an alternative.

He starts by looking at the use of resources for file handling instead of something like file_get_contents. Along with this, however, comes "less happy" things to do around cleanup of the resource in case of error or when complete. He suggests that it can be better handled and, after comparing a PHP function version to a Go function doing the same, refactors to make use of finally to close the resource in one place (and it is always called exception on exception). He refactors it even more by splitting it out into a "cleanup" function that can be reused in other places where resources are accessed.

tagged: finally file resource trycatch cleanup tutorial

Link: https://blog.frankdejonge.nl/finally-file-streams-and-deferred-execution-in-php/

PHPMaster.com:
Lesser-Known "Features" of PHP's OO Model
Jul 22, 2013 @ 17:21:22

On PHPMaster.com there's a new tutorial from Lorna Mitchell about some of the lesser known OOP features that are built in to the PHP language. She talks about things like interface inheritance, private properties and autoloading and type hints.

The vast majority of today’s applications written in PHP are object-oriented, and in general the core OOP concepts are pretty well understood by PHP developers. This article pushes the boundary of your understanding and shows you some tricks, or potential pitfalls depending on your perspective, of OOP in PHP.

Besides the ones mentioned above, she also looks at abstract classes and their use as well as the use of "finally" to handle the cleanup after exceptions.

tagged: features oop model language private inheritance typehint finally

Link: http://phpmaster.com/lesser-known-features-of-phps-oo-model

Joshua Thijssen:
PHP5.5: Try/Catch/Finally
Feb 12, 2013 @ 16:03:23

Joshua Thjissen has a new post to his site today about a feature that's been introduced in the upcoming PHP 5.5 release of the language - the addition of "finally" to try/catch exception handling. He gets into it a bit more technically than just the "introductory" level, discussing parent/child exception handling and using returns.

Exception handling is available in PHP since version 5. It allows you to have a more fine-grained control over code when things go wrong ie, when exceptions occur. But since PHP 5.5, exception handling has finally evolved into what it should have been from the beginning: the "finally" part has been implemented.

He includes a basic example showing how a certain part is always executed, regardless of if the exception is thrown or not. He also shows how a "chained catch" would work to catch multiple kinds of exceptions and when the "finally" is run as it relates to the "trickle down" handling of exceptions. He then gets a little more complex and introduces "return" into the mix. Of special note, even if you return, the "finally" will still get called.

tagged: try catch finally handling exception parent return

Link:

Aaron McGowan:
Finally generators exist as of PHP 5.5
Jan 17, 2013 @ 18:37:27

In this new post to his site Aaron McGowan talks about new features of the upcoming PHP 5.5 release - the "finally" keyword and generators.

PHP 5.5 has recently been released as an ALPHA release, meaning there are still bugs, code is being tested and features being added. With the 5.5 release, many of us PHP developers have a few wonderful new features that we should be taking advantage of almost immediately.

He gives brief introductions to these two new features, including some code examples (but getting a bit more into the generators side of things). You can find out more about these two features and how to implement them when PHP 5.5 comes around from the PHP sitel: generators and finally (actually from the PHP wiki).

tagged: finally generators introduction version update

Link:

Sherif Ramadan:
Finally Getting finally In PHP?
Aug 09, 2012 @ 15:53:55

In this recent post to his site Sherif Ramadan looks at a proposal that's currently under view (RFC) to add the "finally" keyword to PHP.

It’s quite possible that PHP may finally be getting the addition of the finally block in its try/catch block. [...] It also solves a simple, but overlooked problem for the developer. With finally we offer the user-space code a chance to do any clean up work that may be necessary after a try block has terminated execution and with clear semantics.

He includes a use case for this feature - an example showing exception handling on multiple levels and writing to log files when the cleanup of the exception is finished (without the potential for another method to trigger the exception itself). "Finally" allows you to take this logic out of the exception handling and put it at the end, removing the possibility of it triggering an exception for the wrong reason. There's a few other examples showing some other quirks with its usage - like calling die will not make it end up in the "finally".

tagged: finally rfc keyword exception handling proposal

Link:


Trending Topics: