Author: Manuel Lemos
Viewers: 471
Categories: PHP community, News
Please read this short article to learn about what is planned to improve the features of the PDO driver classes so you can prepare to change your PHP code to benefit from planned improvements when they are made available in a PHP version that will be released with these improvements.
In this article you will learn:
1. What Are the Planned Changes to the PDO Driver Classes
1.1 Expose Database Type Specific Functions
1.2 Driver Specific PDO Subclasses Will Be Returned by the connect Function
2. How You Can Benefit From the Planned Changes to the PDO Driver Classes
3. When These Changes are Planned to be Available in a Stable PHP Version
1. What Are the Planned Changes to the PDO Driver Classes
PDO is PHP extension that PHP developers can use to connect to many types of databases that support executing SQL queries. That is fine if you want to write database independent applications.
If you want to take advantage of the features supported only by specific types of databases, the current implementation of the PDO extension is not helpful.
These changes planned for the future implementation approved on July of 2023, will provide two changes:
1.1 Expose Database Type Specific Functions
PDO is a PHP extension that PHP developers can use to connect to many databases that support executing SQL queries. That is fine if you want to write database-independent applications.
If you want to take advantage of the features supported only by specific types of databases, the current implementation of the PDO extension is not helpful.
These changes planned for the future implementation approved on July of 2023, will provide two changes:
1.2 Driver Specific PDO Subclasses Will Be Returned by the connect Function
When you call the connect function of the PDO class, it usually creates a connection to the database.
In the future, the connect function will return driver-specific classes using a static function that will work like a factory. Here is an example taken from the proposal RFC (Request for Change) page.
class PDO { public static function connect(string $dsn [, string $username [, string $password [, array $options ]]]) { if (connecting to SQLite DB) { return new PdoSqlite(...); } return new PDO(...); } }
This possibility will help developers reduce the code necessary for handling database differences between multiple types of databases they want to support in their applications.
The database-specific driver classes may already handle those differences. This way, developers need to call the main PDO class, simplifying their applications.
2. How You Can Benefit From the Planned Changes to the PDO Driver Classes
These changes will be made available in a way that will not affect current PHP applications that use the PDO driver classes.
If you want to take advantage of these changes when you want to implement applications that use PDO and support different types of databases, you can use the PHP method_exists xxx function on the PDO object.
For instance, if your application supports different types of databases, including the PostgreSQL database, and you want to retrieve database records that may contain large objects (LOB), which are a way for a database to store files, you can use the method_exists function, check if the lob_open role like this:
$connection = "pgsql:host=localhost;charset=UTF8'; $pdo = new \PDO( $connection, $username, $password, [] ); // ... if(method_exists($pdo, 'lob_open') { // ... code to process large objects (LOBs) }
3. When These Changes are Planned to be Available in a Stable PHP Version
According to the RFC document the planned changes will be made available in PHP 8.3.
However the status of the proposal is just accepted. Since the PHP 8.3 feature freeze ended in July 18, it is not clear if this feature will still be made available in PHP 8.3.
PHP 8.3 is expected to be release around November or December of 2023.
You need to be a registered user or login to post a comment
Login Immediately with your account on:
Comments:
1. Thank you - Marc Humer (2023-11-16 06:57)
Just a thank you :)... - 1 reply
Read the whole comment and replies