Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88966 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 85259 invoked from network); 28 Oct 2015 14:41:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Oct 2015 14:41:29 -0000 Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.44 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.44 mail-wm0-f44.google.com Received: from [74.125.82.44] ([74.125.82.44:35451] helo=mail-wm0-f44.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id C7/92-63642-79ED0365 for ; Wed, 28 Oct 2015 09:41:28 -0500 Received: by wmll128 with SMTP id l128so8301856wml.0 for ; Wed, 28 Oct 2015 07:41:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-type:content-transfer-encoding; bh=X7iYALYpG8bRFPvKbJdaM+YdjZPG52k+G1YlB/2zAIQ=; b=iaqaPgixlImXmW251lWQ0C2Il5ei7LsCTd48p7KO6EZpQ5smNnlx4R4nGJOcf3ys+E Aclxeoo221xkrRYyuwNCLPWh8PluiGb31zZHz6CzDepk0re7fIXw6zLp0OeM1JV1u45z YhcGmGJ6LYY0LC5z4LNgB9HF4snSUkMw79Zx6rN6fgIkouuoleFwUFqB2YkRh4ixGhXq gHeExeHtQUi2lx7VhrF8lqoezOAkZQNT2N2NiQlWOJCMD6xPVhsPIeuyJGk51p1BMNe+ pg4QF97mQ6CjIBo5ulg2hJAMm5PmwOPH/brn37TXme7KeBqcWwK14HHqKxDI4KPHQGSS cHqA== X-Received: by 10.28.211.70 with SMTP id k67mr1567842wmg.38.1446043285032; Wed, 28 Oct 2015 07:41:25 -0700 (PDT) Received: from [192.168.0.143] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id d81sm4642422wma.16.2015.10.28.07.41.24 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 28 Oct 2015 07:41:24 -0700 (PDT) To: internals@lists.php.net, erik@evanv.nl References: Message-ID: <5630DE79.8070205@gmail.com> Date: Wed, 28 Oct 2015 14:40:57 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight? From: rowan.collins@gmail.com (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() { logTransactionTwo(); }); > $scope->onFailure(function() { rollbackTransactionTwo(); }); > $scope->onExit(function() { cleanupTransactionTwo(); }); > doTransactionTwo(); > } > catch (\Throwable $e) { > $scope->callFailureHandlers(); > throw $e; > } > finally { > $scope->callExitHandlers(); > } > > $scope->callSuccessHandlers(); Did you see Johannes' suggestion re explicit success vs implicit failure? $scope = new ScopeGuard; $scope->onSucces(function() { logTransactionOne(); }); $scope->onFailure(function() { rollbackTransationOne(); }); doTransactionOne(); $scope->onSuccess(function() { logTransactionTwo(); }); $scope->onFailure(function() { rollbackTransactionTwo(); }); $scope->onExit(function() { cleanupTransactionTwo(); }); doTransactionTwo(); $scope->registerSuccess(); 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 $this->callExitHandlers(); } $this->callExitHandlers(); No boilerplate throw-catch needed, works even if there is already a catch or finally within the function's definition. In fact, I kind of want to use this somewhere now... ;) Regards, -- Rowan Collins [IMSoP]