Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:90840 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40638 invoked from network); 22 Jan 2016 19:19:37 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Jan 2016 19:19:37 -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 74.125.82.51 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.51 mail-wm0-f51.google.com Received: from [74.125.82.51] ([74.125.82.51:38347] helo=mail-wm0-f51.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id A1/98-12955-8C082A65 for ; Fri, 22 Jan 2016 14:19:36 -0500 Received: by mail-wm0-f51.google.com with SMTP id b14so147527195wmb.1 for ; Fri, 22 Jan 2016 11:19:36 -0800 (PST) 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=fy86dQ2URWNvZ/HpcYMNmu7hYbzt7labYRd2MdOTHnc=; b=t7ja4LPeFXha285Yi95XgDpxms8JGqk7KYEXIumC10CJ+n8nc1vKorb0zLtnOPDZ73 aeIFAyCA0H4nc+VMuxfSmLVesteRm8CbvCFVhjhIf5Ro+LcY19dHqYQlxY5UDHlSBU9q gpFzJj/Wa+8FKCWiUWoCCwiVuUjBHvwQRWAxpS0xNQlAkJhWS+AmtlXpZybjer/eyj56 5idzePGICA1LXiugWrMAODGd4hPcCxnZON5DoNM/g3XhFYNcqzK+WaOfJ3GiaymLJ6l1 i4p1P43nvjmbGUuEXe0CtBs2HXZJgDnc/Jj6OckUB80B6IM44YykrKOiQV5OBmsdkJbI ydHw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-type :content-transfer-encoding; bh=fy86dQ2URWNvZ/HpcYMNmu7hYbzt7labYRd2MdOTHnc=; b=SQr8VJuZOkMzstgdKlEFRKWNa71hLOMC71mkjZyMWDwfd/RGgDYYRu1XrNBujcO74m lyUn+5lqdKB7XWwtyL36M4vxuMOGxQg2eIHog5NAVRvddv4GRBua1nfF6SNYB8O2o+8Z LjazLk5FIQl+zYo+vUlj9Aeav1I8XtOfCN4/0NICq++KrE9QmqRFLtE93h+LIPsY8aXC wY099sgSlmaSi5IA+/1o161Kpt+JsDQ/wGFHp9vapNdlOWqq6xD6BmnlQ1ijQQsvThgV hpHDjZ2Sjd3kkZxxz3CcS9fmzxXvJ+UG+0FsyBW7ft+RxuqigYhmfrIWBTFOxk+LynJF VRrg== X-Gm-Message-State: AG10YOQOi+wT2TuuD1CeEL/wkpjbt6Ufr2G25qRH2QYI5o5VaUNCjLnq8MVQgK3DwC/W6g== X-Received: by 10.28.64.137 with SMTP id n131mr5027659wma.96.1453490373224; Fri, 22 Jan 2016 11:19:33 -0800 (PST) Received: from [192.168.0.177] ([93.188.182.58]) by smtp.googlemail.com with ESMTPSA id 67sm4204418wmp.20.2016.01.22.11.19.32 for (version=TLSv1/SSLv3 cipher=OTHER); Fri, 22 Jan 2016 11:19:32 -0800 (PST) To: internals@lists.php.net References: Message-ID: <56A2804F.4010006@gmail.com> Date: Fri, 22 Jan 2016 19:17:35 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.5.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] Re: PDO Close Connection From: rowan.collins@gmail.com (Rowan Collins) Gabriel Zerbib wrote on 22/01/2016 13:35: > I hear your point, but I think that the problem is mostly similar to the > following piece: > > $fp = fopen('filename', 'w'); > fclose($fp); > fwrite($fp, 'data'); Sure, but does that mean that PDO should gain a close function, or that the file functions would be better if they didn't have it? That is effectively the question we're discussing. > Suppose you opened a file for writing, and then you're done with it (so > that other processes can have it), but your script must then enter a > time-consuming loop, or sleep, or wait blockingly for another resource. You > don't want to lock stalled the initial file when you know you're finished > with it, therefore you call fclose. And you already know, that, should you > need it again, you would call fopen again. If you have a single reference to the file pointer in your code, then this is true, and is equivalent to the current unset($pdo) case. When you need it again, you call new PDO() again. If you, as you described in your original post, "end up never knowing what other component of the application still retains a reference", then running fclose($fh) seems very dangerous indeed - none of those places know that next time they read from the file handle, they need to reopen the file to avoid an error; indeed, they probably don't know *how* to reopen the file. Most will just assume that, because they were given a file handle, rather than creating their own, that file handle is and will remain valid. > Besides, expecting from PHP users that they write a Facade/Singleton > pattern to wrap a core class and proxy all its publics, seems high > standards :) It doesn't seem that complex to me. I just hacked together a version (untested, and unoptimised) in 22 lines of code which implements the "close and then error forever" pattern: https://gist.github.com/IMSoP/bb0aecf214071a5b59cb This would be really easy to add to core, but also not that useful - do you really want every class which has a reference to that object to generate a stream of exceptions when they try to query? To actually make it useful, you need to have something you can do about it, like an openConnection() method. Here's a more complex wrapper, still only 30 lines of code, which implements a lazy connection (connect on first use, reconnect on first use after closing): https://gist.github.com/IMSoP/a8e329e81eacd22a66c1 But this is probably missing features, which different people might want to do in different ways, e.g. if you call setAttribute(), should the option stay set after a close() / open() or be reverted (I can imagine use cases for both ways around)? Should the connection automatically reopen itself like this, or should it throw an exception and expect the caller to call open()? Should the connection be opened on construct, or on first use? Should there be an accessor for isOpen(), or an ensureOpen()? And if this class replaced PDO, would we be storing credentials in memory which are normally used once at connection time and then discarded? As ever, the devil is in the detail. PDO in general operates at quite a low-level, rather than being an opinionated implementation. The joy of the PHP ecosystem right now is that there are tons of nice libraries out there, and you don't have to wait for a new PHP version to get a bugfix or feature, just "composer update". Regards, -- Rowan Collins [IMSoP]