On Tue, 30 Sept 2025 at 15:42, Andrey Andreev <[email protected]> wrote: > > On Tue, Sep 30, 2025 at 4:56 PM Kamil Tekiela <[email protected]> wrote: >> >> I find this proposal to be a backwards step in the age when we are >> moving away from resources and their explicit closure. We have >> disabled this in PHP 8.0 for curl_close(), imagedestroy(), >> openssl_pkey_free(), shmop_close() and xml_parser_free(). Sure, >> mysqli_close() still works, but the whole extension is mostly a >> historical artefact and a bunch of bad design choices due to what that >> extension is (quick replacement of mysql_* API). PDO was designed much >> better and it deliberately avoided the foot gun that is explicit >> connection closure. I really wish we would not introduce bad design >> choices into the language, and because of this, I wish this proposal >> were unsuccessful. > > > All of the foot gun examples you listed are such because that historic design > pattern *required* explicit resource frees, and they are foot guns because it > is easy to forget doing that. > Also, mysqli_close() is the only one that handles a stateful network > connection and not a local resource, which is a good reason why it still > works. > > There are legitimate use cases where you may want to explicitly and > immediately close a network connection, even if PHP would do it eventually. > > Cheers, > Andrey.
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.
