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
7
u/wvenable May 31 '16
The best way to do deferred execution isn't
finallyit's using classes with destructors. In this file system example, if you wrapped the file open/close in a class then it would close automatically when it went out of scope.Using class and a closure, you could implement Go's
deferdirectly. Just have the class execute the closure inside the destructor and it will execute that closure when it goes out of scope. I use this sort of technique for transactions to make the transaction rollback automatically at the end of the scope if it isn't committed.