Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:88977 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 24093 invoked from network); 28 Oct 2015 23:18:03 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 28 Oct 2015 23:18:03 -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:38105] helo=mail.experimentalworks.net) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id B2/32-08716-AA751365 for ; Wed, 28 Oct 2015 18:18:03 -0500 Received: by mail.experimentalworks.net (Postfix, from userid 1003) id 72EB64F062; Thu, 29 Oct 2015 00:18:04 +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=-1.0 required=4.0 tests=ALL_TRUSTED autolearn=unavailable autolearn_force=no version=3.4.0 X-Spam-HAM-Report: * -1.0 ALL_TRUSTED Passed through trusted hosts only via SMTP 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 192F74F05F; Thu, 29 Oct 2015 00:18:02 +0100 (CET) Message-ID: <1446074274.4127.82.camel@kuechenschabe> To: Erik van Velzen Cc: internals@lists.php.net Date: Thu, 29 Oct 2015 00:17:54 +0100 In-Reply-To: References: <5630DE79.8070205@gmail.com> Content-Type: multipart/signed; micalg="pgp-sha1"; protocol="application/pgp-signature"; boundary="=-6tVSvvUSA2z03MRC2k3a" 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?=) --=-6tVSvvUSA2z03MRC2k3a Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable 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 =3D false; protected abstract function fail(); protected function success() {} public function succeed() { if (!$this->succeeded) { throw new RAIIAlreadyMarkedAsSuccessfulException(); } $this->success(); $this->succeeded =3D true; } public function __destruct() { if (!$this->succeeded) { $this->fail(); } } } class CallbackRAIIGuard extends AbstractRAIIGuard { private $success; private $fail; public __construct(callable $fail, callable $success =3D null) { $this->success =3D $success; $this->fail =3D $fail; } protected function fail() { call_user_func($this->fail()); } public function success () { if ($this->success) { call_user_func_array($this->success, func_get_args()); } } } class DBRAII extends AbstractRAII { private $db; public function __construct(DB $db) { $this->db =3D $db; $db->begin(); } protected function fail() { $this->db->rollback(); } public function success () { $this->db->commit(); } } function foo(DB $db) { $guard =3D new DBRAII($db); // or using the more generic class $guard =3D new CallbackRAIIGuard( [$db, "rollback"], [$db, "commit"] ); =20 // .... $guard->succeed(); } johannes --=-6tVSvvUSA2z03MRC2k3a 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) iQEcBAABAgAGBQJWMVejAAoJEH3sTmn8nIPXK8kH/iqWBTva2E73k5LdZzUsio5G k90cfpzyEvW/9NBXTNsJSEKbmtH7hgfQb6QdZs2mtj+qYxbJjM/i5GB6ZhnzYekg ohbxRj52e04lYGkkDOdq+/YLLPBpspZCcKLLD2Q2mBkgtDeOwWIsHlu1/+vUkAIe inbmoiYtDNUWrOSrcIGDulAkD+sXgulBHnNH+7WWUtOj72KHeSXg3Xa4Bih2PPoW H7VDzWAWBLTWfz/dO2pyhPmVBfcGRfhZ3bnDl6J0VwEX/JAkbAUAC4o2WPJgGdwa 7VgyyutTMOoJdgGApaEjm/vXzFWDR4ELAaqA9WtOPYqNwd0oRbh6skmw1wT6XRw= =zQb6 -----END PGP SIGNATURE----- --=-6tVSvvUSA2z03MRC2k3a--