Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-29 Thread Rowan Collins
Johannes Schlüter wrote on 28/10/2015 22:56: On Wed, 2015-10-28 at 16:45 +, Rowan Collins wrote: On the downside, it's worth noting that while PHP destructors are technically deterministic, the language can actually prevent them running on scope exit, because Exceptions hold references to

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Erik van Velzen
Thanks for your input. I did not try-catch because it doesn't really work, you either duplicate rollback code or get excessive nesting. Real code will obviously be more complicated than the examples. Also in both cases the handlers are many lines apart from the actual transaction call. Duplicate

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Johannes Schlüter
On Wed, 2015-10-28 at 11:00 +0100, Erik van Velzen wrote: > Subject says it all. > > I want to make a small "ScopeGuard" library for exception-safe > transactions, like the following: > > $scope = new ScopeGuard; > > $scope->onSucces(function() { logTransactionOne(); }); >

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Erik van Velzen
I didn't see how it could be generalized but your example clears that up. - Erik 2015-10-28 15:40 GMT+01:00 Rowan Collins : > > Did you see Johannes' suggestion re explicit success vs implicit failure? > > $scope = new ScopeGuard; > > $scope->onSucces(function() {

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Rowan Collins
Erik van Velzen wrote on 28/10/2015 13:28: try { $scope = new ScopeGuard; $scope->onSucces(function() { logTransactionOne(); }); $scope->onFailure(function() { rollbackTransationOne(); }); doTransactionOne(); $scope->onSuccess(function() {

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Nikita Nefedov
Hi Erik, On Wed, 28 Oct 2015 16:28:59 +0300, Erik van Velzen wrote: Thanks for your input. I did not try-catch because it doesn't really work, you either duplicate rollback code or get excessive nesting. Real code will obviously be more complicated than the examples. Also in

[PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Erik van Velzen
Subject says it all. I want to make a small "ScopeGuard" library for exception-safe transactions, like the following: $scope = new ScopeGuard; $scope->onSucces(function() { logTransactionOne(); }); $scope->onFailure(function() { rollbackTransationOne(); }); doTransactionOne();

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Rowan Collins
Erik van Velzen wrote on 28/10/2015 15:02: The ScopeGuard's destructor can now detect the condition: if ( $this->success_registered ) { // Function reached checkpoint for this scope $this->callSuccessHandlers(); } else { // Function aborted early, due to a throw or early return

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Johannes Schlüter
On Wed, 2015-10-28 at 16:45 +, Rowan Collins wrote: > On the downside, it's worth noting that while PHP destructors are > technically deterministic, the language can actually prevent them > running on scope exit, because Exceptions hold references to everything > used as a parameter in

Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight?

2015-10-28 Thread Johannes Schlüter
On Wed, 2015-10-28 at 16:02 +0100, Erik van Velzen wrote: > I didn't see how it could be generalized but your example clears that up. Quick attempt of a generalization: abstract class AbstractRAIIGuard { private $succeeded = false; protected abstract function fail(); protected