Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:89000 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 99266 invoked from network); 29 Oct 2015 14:56:08 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Oct 2015 14:56:08 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.212.180 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 209.85.212.180 mail-wi0-f180.google.com Received: from [209.85.212.180] ([209.85.212.180:38260] helo=mail-wi0-f180.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 42/61-25356-78332365 for ; Thu, 29 Oct 2015 09:56:08 -0500 Received: by wicll6 with SMTP id ll6so45022667wic.1 for ; Thu, 29 Oct 2015 07:56:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=subject:to:references:from:cc:message-id:date:user-agent :mime-version:in-reply-to:content-type:content-transfer-encoding; bh=zTtf0zbikHj+aNbMfh6KVBHCn0H4c2CBPFEoy1OViIc=; b=NOzNXozhHyVH1BOsOLG9CuRVMQSmQhK4qwttbKKybQptAhd9yo55iKPteQOzMJKpVq qv63gvbMOiDK9DoNiKUFI3dKXRfBCTShSUwouXlxblMoI/sgBeSw/SaqwCAH79jN/7V6 hxnziJiNpa4/R/f9+/O4Qk6NoK9tfa9mizuMHMhTrjp6KP7dJ/6oTdrIB45QMOxtE4s+ VBzPKS6rndbok17CawkqlvmYtXtIkYUjScjNLLml218wTF51J7EhEia790AXIZqunOqC GrfiHJya693OO/Y5my1w/phXWd1ONT9Z5vYkcJ5aaxoDhBZVnsTF3B21kQ0KOq6vfkC9 5w3w== X-Received: by 10.194.235.37 with SMTP id uj5mr3017904wjc.115.1446130565203; Thu, 29 Oct 2015 07:56:05 -0700 (PDT) Received: from [192.168.0.143] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id j4sm3895094wmg.18.2015.10.29.07.56.04 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 29 Oct 2015 07:56:04 -0700 (PDT) To: internals@lists.php.net References: <5630DE79.8070205@gmail.com> <5630FBC1.4000806@gmail.com> <1446072986.4127.63.camel@kuechenschabe> Cc: =?UTF-8?Q?Johannes_Schl=c3=bcter?= Message-ID: <56323364.6040707@gmail.com> Date: Thu, 29 Oct 2015 14:55:32 +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: <1446072986.4127.63.camel@kuechenschabe> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: [PHP-DEV] In a destructor, how to detect if an exception is currently in flight? From: rowan.collins@gmail.com (Rowan Collins) Johannes Schlüter wrote on 28/10/2015 22:56: > On Wed, 2015-10-28 at 16:45 +0000, 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 their stack traces. >> >> In the below very simple example, both err() and foo() have completed, >> but the destructor fires only once the Exception is caught and >> discarded. So if you passed your ScopeGuard object around at all, you >> might find things executing in a surprising order. > That's an interesting case, I in fact didn't think about, but I believe > a scope guard / RAII object should not be passed to another scope. It's definitely an edge-case, and a class dedicated to RAII as an abstract concept would probably never hit it, but if you consider RAII as just one aspect/"trait" of another object, it could be problematic. Note that the function throwing the Exception need not be interacting directly with the object: function do_something_with_a_file($filename) { $fh = new RAIIFileHandle($filename); // ... $this->refactored_code_which_got_too_long($fh); // ... } function refactored_code_which_got_too_long($fh) { // ... $this->get_db_lookup_table(); // ... } function get_db_lookup_table() { if ( ! $this->db ) { throw new DB_Connection_Exception; } } The assumption is that an instance of RAIIFileHandle will close the underlying file handle as soon as control leaves do_something_with_a_file; this assumption holds across the refactoring to multiple functions. However, if the DB is disconnected, the file handle stays open until the stack has rewound to some generic error handler, because the stack trace contains an entry for refactored_code_which_got_too_long complete with a counted reference to its parameter. Thinking about it, this example probably holds for built-in Resource types as well. Regards, -- Rowan Collins [IMSoP]