Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:90831 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 9329 invoked from network); 22 Jan 2016 13:36:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 22 Jan 2016 13:36:17 -0000 Authentication-Results: pb1.pair.com smtp.mail=gabriel.zerbib@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=gabriel.zerbib@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.174 as permitted sender) X-PHP-List-Original-Sender: gabriel.zerbib@gmail.com X-Host-Fingerprint: 209.85.214.174 mail-ob0-f174.google.com Received: from [209.85.214.174] ([209.85.214.174:33259] helo=mail-ob0-f174.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 75/64-12955-05032A65 for ; Fri, 22 Jan 2016 08:36:16 -0500 Received: by mail-ob0-f174.google.com with SMTP id is5so62474492obc.0 for ; Fri, 22 Jan 2016 05:36:16 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:from:date:message-id:subject:to:content-type; bh=XnYMB9XPHF0LOIxnT4wYSwOdzqDkImNFtddIgWgHong=; b=yM1/0FmvJLBv68+TVhyGOUTCTzsMWQfOYelPELA1m8qD1AioGjo53jMUbOj5JbTSMa iBkUz4fNXiskMacX1puV22dVaXhnB836n24MAl3sogG7l7eEaR5+t0pB7HSPJaoDRmTX LMgvnPuCFLgZ4t0fIw7xtzx0PS5bybwgv7VXfrNDqX1f9HdXegPAshEBmDMTaRx1yeI7 Oc7lZKAYUVLyjg3LfZDmZeoVgKhVY5f5VGtwT67bBCOHTKg8aw4wwdSeCCFJ3YGkoc2F d2nm+YwUCYMLmq2HDEauArVY+AlYYtFNRgVlfvI8y+V/fN5HkCRnkVl8+8kckKwLsYnX YH7w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:from:date:message-id:subject:to :content-type; bh=XnYMB9XPHF0LOIxnT4wYSwOdzqDkImNFtddIgWgHong=; b=Qwfk0gLjayEsdQjqm2gEMSXX1wHeo+Hfi01bmPVKCmE9XeFbAU2tAlMiRfto2GMjwt TzzrNgBoyXXO5AUi6b9QsmqkTZ8k4PlgVAuXwteUf+sb7GUJPOBreahuYgE5iucG2Q4x FNa5WYb3mIYyRZVnh+8nJdCoQP757Y5yAw2eZIB/jFvCW7ZfkFNtmAr3Cgh8sKGp/WmS fSlevCBlEjKu50c1Vinfaf4MVBaUQSZU20ubJngK2EdHpAA/Xt6rWcpHkkhnnr86psX0 UesxNCqGnwpXtEltpGqxrC3ZlHf9gE1IFds1LWWXVzzWgCaWJVZP/0bgJFmKyV7TkZGg JINw== X-Gm-Message-State: AG10YOQYDjGtaNh5I5Mp6VR7RowOWjNJO5ZE8xblwY5uOUGyBdw/XpDvMtdBq2SLHmXHD7uEboOq8klx37awPg== X-Received: by 10.60.36.194 with SMTP id s2mr2293109oej.53.1453469773553; Fri, 22 Jan 2016 05:36:13 -0800 (PST) MIME-Version: 1.0 Received: by 10.202.69.2 with HTTP; Fri, 22 Jan 2016 05:35:54 -0800 (PST) Date: Fri, 22 Jan 2016 15:35:54 +0200 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=089e011763118b65740529ec4dc4 Subject: Re: PDO Close Connection From: gabriel.zerbib@gmail.com (Gabriel Zerbib) --089e011763118b65740529ec4dc4 Content-Type: text/plain; charset=UTF-8 > > > ---------- Forwarded message ---------- > From: Rowan Collins > To: internals@lists.php.net > Cc: > Date: Thu, 21 Jan 2016 19:18:51 +0000 > > > I think the main problem with a close method on something that represents > a resource like a PDO connection object is how the object should behave > *after* it has been closed. That is, what should the following code do? > > $pdo = new PDO($dsn); > $pdo->closeConnection(); > $pdo->query('Select 1 as test'); > > 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'); In other words, the developer who explicitly closes a resource, knows why he is doing it, supposedly. > The object needs to track the fact that it is in a closed state, and do > something - maybe throw a ConnectionAlreadyClosedException? > I think that the PHP developers are quite familiar with this when it comes to files or sockets, and people like to use fclose when they think it's needed. 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. > So that means the close() method needs to be accompanied by a reopen() > method, which means saving the DSN and credentials > This use case remains true, if there are other references to $fp elsewhere in the code. The underlying resource is simply freed, and the object did not keep track of the path or opening mode, etc. > > One solution to this is to create a wrapper around PDO, which is the only > thing allowed to have a direct reference to the PDO object itself. > I think you are perfectly right when it comes to design patterns and good practices. But alas not every PHP user will or can implement them in this way. Even when doing things properly, e.g. using a stable and popular ORM like Doctrine, I could end up writing: $pdo = $entityManager->getConnection()->getWrappedConnection(); and then even if Doctrine offered a way to nullify its internal PDO reference, I ruined it. 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 :) Of course a PDO closeConnection method is not the hottest emergency, but all I'm saying is that I could use it... ! Thanks, Regards, Gabriel --089e011763118b65740529ec4dc4--