PHP RFC: PDO driver specific sub-classes
Introduction The article discusses a proposal to enhance the PDO (PHP Data Objects) class, which is a generic database class in PHP. Currently, PDO supports various databases, each with its own specific functionality. However, these functionalities are available on the generic PDO class, making it harder to reason about the code. The proposal suggests adding subclasses of PDO for each database, allowing for a more organized and intuitive structure.
Proposal The proposal consists of two parts. Firstly, new subclasses of PDO will be created for each PDO extension in the PHP source repository. These subclasses will expose functionalities specific to each database, such as PdoSqlite::createFunction() for SQLite. Secondly, a static factory method called connect
will be added to PDO. This method will determine the exact type of database being connected to and return the appropriate sub-class instead of a generic PDO object.
Code Example:
$pdo = PDO::connect($dsn, $username, $password);
Users will also be able to directly create the database-specific classes through the appropriate constructor.
Code Example:
$pdo = new PdoSqlite($dsn, $username, $password);
Sqlite Extension Directory INI Setting The article also addresses the issue of the Sqlite3 extension requiring users to set an INI setting to allow SQLite extensions to be loaded. The proposal suggests that this requirement may not be necessary, as the vulnerability only arises if someone is able to upload and execute PHP code on a server, in which case the server is already compromised.
Backward Incompatible Changes There are no known backward incompatible changes resulting from this proposal. However, developers who are currently extending the PDO class directly and wish to extend the specific types will need to write their own connect functions that wrap and proxy those specific types.
Proposed PHP Version This proposal is intended for PHP 8.3.
RFC Impact to SAPIs The impact of this proposal on various SAPIs (Server Application Programming Interfaces) such as CLI, development web servers, and embedded PHP is yet to be determined.
Open Issues There are currently no known issues related to this proposal.
Unaffected PHP Functionality This proposal does not affect any PHP functionality outside of PDO.
Overall, the article highlights the proposal to introduce PDO driver specific sub-classes in PHP. This enhancement aims to improve code organization and make it easier for developers to work with specific database functionalities. The proposal also includes a static factory method and addresses the issue of the Sqlite3 extension's INI setting. Developers interested in the latest PHP developments should keep an eye on this RFC for potential changes in PHP 8.3.