Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[11.x] Add PDO subclass support for PHP 8.4 #52538

Merged
merged 3 commits into from
Aug 22, 2024

Conversation

ju5t
Copy link
Contributor

@ju5t ju5t commented Aug 20, 2024

Description

In PHP 8.4 there will be PDO driver specific subclasses. PHP will introduce classes for each database driver it supports. It allows for driver specific methods. I don't think this will be a huge improvement for most, but it does allow SQLite users to load extensions for example. This is not available in PDO at the moment.

If you're not using Laravel this would look like this:

$pdo = PDO::connect('sqlite:database.sqlite');
$pdo->loadExtension('sqlean.dylib');

$query = "select regexp_replace(company_name, '[^a-zA-Z0-9]', '') from customers";
$query = $pdo->query($query)->fetchAll();

I'm not sure what your policy is for features that are only in 8.4, so please let me know what I could do to improve on this. I've targeted this at 11.x as it's backwards compatible with older PHP versions.

Backwards compatibility

PDO::connect returns a subclass of PDO. It will pick the correct subclass for you based on the DSN. As the subclass extends PDO, the return type does not need to change and is backwards compatible.

As PDO::connect is not compatible with versions prior to 8.4. This PR is backwards compatible by using new PDO for other PHP versions instead.

Tests

I haven't included any new tests. PDO::connect is a PHP function so it should not require testing in userland. The method itself is already covered by the test suite and its functionality remains the same.

Edit: static analysis failed because it runs on 8.2 where PDO::connect does not exist yet.

References

Copy link

Thanks for submitting a PR!

Note that draft PR's are not reviewed. If you would like a review, please mark your pull request as ready for review in the GitHub user interface.

Pull requests that are abandoned in draft may be closed due to inactivity.

@ju5t ju5t marked this pull request as ready for review August 20, 2024 20:53
@ju5t ju5t changed the base branch from 11.x to master August 20, 2024 20:54
@ju5t ju5t changed the base branch from master to 11.x August 20, 2024 20:54
@ju5t ju5t changed the title [12.x] Add PDO subclass support for PHP 8.4 [11.x] Add PDO subclass support for PHP 8.4 Aug 21, 2024
Co-authored-by: Julius Kiekbusch <contact@julius-kiekbusch.de>
@taylorotwell
Copy link
Member

How do we fix that static analysis error? 🤔

@ju5t
Copy link
Contributor Author

ju5t commented Aug 22, 2024

@taylorotwell I don't know enough about phpstan's internals to say anything meaningful about it. But I don't think you can unless you run static analysis on 8.4. And that's probably not a good idea in 11.x or 12.x.

The only 'workaround' I can think of is ignore the error for now, and add a PHP 8.4 test here. We can test if the PDO instance is Pdo\Sqlite, which is the subclass for SQLite in 8.4.

I'm not a fan of ignoring the error but this is the best I can think of.

@ondrejmirtes perhaps you know of a better solution?

@driesvints
Copy link
Member

@ju5t just add a ignore line to it for now 👍

@taylorotwell taylorotwell merged commit 8a4a52e into laravel:11.x Aug 22, 2024
29 checks passed
@ju5t ju5t deleted the add-pdo-subclass-support branch August 22, 2024 13:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants