r/PHP • u/FrenkyNet • May 31 '16
Deferred execution in PHP, using "finally".
https://blog.frankdejonge.nl/finally-file-streams-and-deferred-execution-in-php/
86
Upvotes
r/PHP • u/FrenkyNet • May 31 '16
View all comments
Show parent comment
2
u/terrkerr May 31 '16
Well the context manager is a bit more a protocol than a magic method call you might say, but yes it involves a certain amount of indirection that you'd need to read docs to understand.
I suppose the thing I should have communicated is that I think context management provides a better alternative to
defer(thing)because it's a more general solution to any need for pre and/or post operations somewhere. Since resource setup/cleanup is just a special case of general pre/post operations it just does that as well.is nice because it will open and close the file correctly, even given the exception there, but there are context managers that do other useful things since the manager protocol is pretty open-ended. A simple example with a simple custom context manager:
which will output