PHP Classes

How PHP PDO Driver Classes Will Improve in Future PHP Versions

Recommend this page to a friend!
  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How PHP PDO Driver Cl...   Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)  

Author:

Updated on: 2023-09-05

Posted on: 2023-09-05

Viewers: 471 (September 2023)

Categories: PHP community, News

The PHP core group has approved a new proposal to improve the features of the PDO driver classes.

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.




Loaded Article

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

PHP 8.3

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



  Blog PHP Classes blog   RSS 1.0 feed RSS 2.0 feed   Blog How PHP PDO Driver Cl...   Post a comment Post a comment   See comments See comments (2)   Trackbacks (0)