Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88963 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 72380 invoked from network); 28 Oct 2015 11:13:43 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Oct 2015 11:13:43 -0000 Authentication-Results: pb1.pair.com smtp.mail=johannes@schlueters.de; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=johannes@schlueters.de; sender-id=unknown Received-SPF: error (pb1.pair.com: domain schlueters.de from 217.114.215.10 cause and error) X-PHP-List-Original-Sender: johannes@schlueters.de X-Host-Fingerprint: 217.114.215.10 mail.experimentalworks.net Received: from [217.114.215.10] ([217.114.215.10:35238] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 83/11-63642-4EDA0365 for ; Wed, 28 Oct 2015 06:13:41 -0500 Received: by mail.experimentalworks.net (Postfix, from userid 1003) id 64C7C4EFF7; Wed, 28 Oct 2015 12:13:43 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on km31408.keymachine.de X-Spam-Level: X-Spam-Status: No, score=-0.3 required=4.0 tests=ALL_TRUSTED,URIBL_SBL, URIBL_SBL_A autolearn=no autolearn_force=no version=3.4.0 X-Spam-HAM-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP * 0.1 URIBL_SBL_A Contains URL's A record listed in the SBL blocklist * [URIs: slides.com] * 0.6 URIBL_SBL Contains an URL's NS IP listed in the SBL blocklist * [URIs: slides.com] Received: from [192.168.2.34] (ppp-93-104-6-87.dynamic.mnet-online.de [93.104.6.87]) (using TLSv1.2 with cipher DHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) (Authenticated sender: johannes@schlueters.de) by mail.experimentalworks.net (Postfix) with ESMTPSA id 520714EFF7; Wed, 28 Oct 2015 12:13:41 +0100 (CET) Message-ID: <1446030814.4127.60.camel@kuechenschabe> To: Erik van Velzen Cc: internals@lists.php.net Date: Wed, 28 Oct 2015 12:13:34 +0100 In-Reply-To: References: Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-mEDH8sN5HY574czK3R2b" X-Mailer: Evolution 3.10.4-0ubuntu2 Mime-Version: 1.0 Subject: Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight? From: johannes@schlueters.de (Johannes =?ISO-8859-1?Q?Schl=FCter?=) --=-mEDH8sN5HY574czK3R2b Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Wed, 2015-10-28 at 11:00 +0100, Erik van Velzen wrote: > Subject says it all. >=20 > I want to make a small "ScopeGuard" library for exception-safe > transactions, like the following: >=20 > $scope =3D new ScopeGuard; >=20 > $scope->onSucces(function() { logTransactionOne(); }); > $scope->onFailure(function() { rollbackTransationOne(); }); > doTransactionOne(); >=20 > $scope->onSuccess(function() { logTransactionTwo(); }); > $scope->onFailure(function() { rollbackTransactionTwo(); }); > $scope->onFailure(function() { logFailureOfTransactionTwo(); }); > doTransactionTwo(); >=20 > // ... Three Four etc... >=20 > So if an exception is thrown the attached onFailure callbacks will be > executed, if the scope is exited through a return however the success > callbacks will be executed. The way to do this is by marking the success in an explicit way. See http://slides.com/johannes4321/froscon15/#/4 and http://slides.com/johannes4321/froscon15/#/6 With your attempt you can only handle abortion via exception. With a pattern like shown in my slides you can also handle cases where you "return" early nicely. johannes --=-mEDH8sN5HY574czK3R2b Content-Type: application/pgp-signature; name="signature.asc" Content-Description: This is a digitally signed message part Content-Transfer-Encoding: 7bit -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (GNU/Linux) iQEcBAABAgAGBQJWMK3eAAoJEH3sTmn8nIPXweMIALZz0dHMLkCQ8dJ/p4w/co51 o567j4HJ3sw3JZL3vKVDBoc5kFln+gKApFhCain1j7mNBI8FbB/gOFHJtUgmPzQW RZK6hEYERX/d6/XEULchpQWWdev8U3DJri1gMyh45bH4zIW7n/q7b4JRpXR6OR1p kgHgRVcQkv7/qU69Cn8ixP3ULalurQ37PWDtCxiwIzgL0ZetZbtz/hyQfR2MMpYm k7Es3z9Ic01zhNwZnLuy590p3UgCZZs2UxNf1dFNzMgzaFmNK0iuv7H+oidSdNXt /yVc4N+4F+fLOBhVVQj2KYky624rIUgYZxPkcZcL1BW6yk9GwGEq/1TdYZcXQJM= =rFd/ -----END PGP SIGNATURE----- --=-mEDH8sN5HY574czK3R2b--