On Tue, Sep 30, 2025, at 10:13 AM, Kamil Tekiela wrote: > By foot gun, I meant that when such a method exists, the developer can > never be sure that the PDO object is in a consistent state. As long as > the PDO object is accessible, closing it in one place in the code > means that there exists another place in the code where the object is > accessible but unusable. This is what gets so many mysqli users. > > Another issue is that if you call disconnect on PDO object, but you > still have PDOStatement with unfetched data, you silently lose the > data or get an exception. This situation is more difficult to > accidentally arrive at, but still possible. You also lose the ability > to call methods such as PDOStatement::execute(). This is not possible > today, but by adding disconnect method we introduce a way in which > this is possible. > > PDO isn't a new extension. It has done well without such a function > for many years. Maybe there are legitimate use cases for this, but > there are also alternatives. Pretty much every time I hear the > argument that it would be nice if PDO had it, I can reply with: you > can just design your code in a way that doesn't need it. > > The disconnect method isn't needed for long-running scripts. PHP will > disconnect automatically when it determines that the connection is no > longer needed, and only then. It's the responsibility of the developer > to ensure that they don't keep variables around that aren't necessary > anymore.
I generally agree that there's little value to a disconnect() action; unset($pdo) is already sufficient. What would be valuable is easier RE-connection logic. Either PHP or MySQL may terminate an idle connection, which is a concern in long-running processes (Queue workers, ReactPHP, Franken, etc.). Tools like Doctrine have built their own refresh logic in, although they're not always consistent with each other or obvious to use. Having some kind of reconnection logic built into PDO that "just works" would be very valuable. What that would look like, I'm not sure. Just spitballing: // After X seconds, drop and reopen the connection the next time it's used. $pdo->reconnectAfter($seconds); --Larry Garfield
