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

Correct behaviour to match PHP 8.4 PDO returned types for connect and __construct for the internal pdo object #231

Open
wants to merge 3 commits into
base: 6.x
Choose a base branch
from

Conversation

srjlewis
Copy link
Contributor

I have done some changes to keep the behaviour the same as the PDO calls with the correct returned types for the pdo object.

Thanks to @frederikbosch as I took some of the ideas from #228 (comment), hope you don't mind

@@ -111,7 +134,11 @@ public function establishConnection(): void
// connect
$this->profiler->start(__FUNCTION__);
list($dsn, $username, $password, $options, $queries) = $this->args;
$this->pdo = PDO::connect($dsn, $username, $password, $options);
if ($this->driverSpecific) {
$this->pdo = PDO::connect($dsn, $username, $password, $options);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we don't need the connect method itself as this is implemented internally in the PDO.

Copy link
Contributor Author

@srjlewis srjlewis Nov 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was looking at the final release of the new PDO

PDO::__construct() always returns PDO and PDO::connect() returns Pdo\{driver} or PDO depending on available drivers

So if you use ExtendedPdo::getPdo() you would get the correct typed object.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That behaviour of PDO is expected. But in our case we are not calling the parent construct.

@aik099
Copy link

aik099 commented Dec 13, 2024

I'm getting this error when trying to use this library on PHP 8.4 ( https://github.com/console-helpers/db-migration/actions/runs/12323306770/job/34398667606 ):

PHP Fatal error:  Cannot make static method PDO::connect() non static in class Aura\Sql\AbstractExtendedPdo in .../vendor/aura/sql/src/AbstractExtendedPdo.php on line 161

I'm hoping this PR will help with that.

@srjlewis
Copy link
Contributor Author

@aik099 what version of PHP are you using and what does your composer file look like?

@aik099
Copy link

aik099 commented Dec 14, 2024

@aik099 what version of PHP are you using and what does your composer file look like?

@srjlewis , I'm using PHP 8.4.

The composer.json file content is ( from https://github.com/console-helpers/db-migration/blob/master/composer.json ):

{
	"name": "console-helpers/db-migration",
	"description": "Database migrations made simple",
	"keywords": ["database", "migration", "sqlite"],
	"license": "BSD-3-Clause",
	"authors": [
		{
			"name": "Alexander Obuhovich",
			"email": "[email protected]"
		}
	],
	"require": {
		"php": ">=5.6",
		"aura/sql": "^2.5 || ^3.0 || ^4.0 || ^5.0"
	},
	"require-dev": {
		"aik099/coding-standard": "dev-master",
		"yoast/phpunit-polyfills": "^2.0",
		"phpspec/prophecy": "^1.10",
		"console-helpers/prophecy-phpunit": "^3.0"
	},
	"autoload": {
		"psr-4": {
			"ConsoleHelpers\\DatabaseMigration\\": "src/DatabaseMigration/"
		}
	},
	"autoload-dev": {
		"psr-4": {
			"Tests\\ConsoleHelpers\\DatabaseMigration\\": "tests/DatabaseMigration/"
		}
	},
	"extra": {
		"branch-alias": {
			"dev-master": "0.1.x-dev"
		}
	}
}

@srjlewis
Copy link
Contributor Author

srjlewis commented Dec 15, 2024

@aik099 you would need to change your composer to

"require": {
	"php": ">=5.6",
	"aura/sql": "^2.5 || ^3.0 || ^4.0 || ^5.0||dev-6.x"
}

this will use 6.x branch that is compatable with PHP 8.4.

I awaitig #230 being merged which would allow swapping between aura/sql versions, this is only targeting 5.x branch but can be backported to previous version.

but yes this PR would help aswell to some degree in your case.

All this is due to PHP 8.4 introducing PDO::connect() this is the issue regarding the changes #228

I have both of these PR's in my personal repo and can easily switch between versions.

@harikt please could you review these PR's and tag the 6.x branch for release, it would be helpful for PHP 8.4 users.

@aik099
Copy link

aik099 commented Dec 15, 2024

@srjlewis , thank you for an explanation. I now understand why it's failing. I'll wait for Aura/Sql 6.x release. We're not switching to PHP 8.4 this year.

@harikt
Copy link
Member

harikt commented Dec 15, 2024

@srjlewis yes, I have been checking this. But I am not sure if we need this change or not. @koriym , @frederikbosch can you guys look into this and give some suggestion if we need this specific change ?

  1. I think connect method can be removed from ExtendedPDO

    public static function connect(

  2. In this we are passing to options driverSpecific, immediate etc which is not PDO specific and can cause more issues in the future with PDO specific changes.

@srjlewis
Copy link
Contributor Author

@harikt,

  1. We need the ExtendedPdo::connect() method, if we remove it you would be able to call ExtendedPdo::connect() and get a PDO object instead of a ExtendedPdo, so we wrap the connect method so we return a ExtendedPdo object consistently

  2. Yes I agree there are other ways to pass the flags, but I did like the idea of how @frederikbosch was added to the options array in Support for PHP 8.4 #228 (comment), also as PHP internals only use INT's as there values, and I am using strings there would be no confilict, and when passed to a PDO object they are just ignored.

}

public static function connect(
string $dsn,
?string $username = null,
?string $password = null,
?array $options = [],
?array $options = null,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can't we actually use [] instead of null, this will also remove the usage of $options ??= []; right ?

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.

5 participants