Declaration of OM\Db::query(string $statement) must be compatible with PDO::query
Solution 1
#1 - Remove from composer Json the similar line
"doctrine/dbal": "^2.10",
#2 - Run
composer upgrade
#3 - Run
composer update
#4 - Voila ! The error should be fixed !
Solution 2
To expand on the error message a bit, the signature of the query
function in your class must be compatible with the parent method in the PDO class.
Where you have this:
public function query(string $statement)
, the parent class has this:
public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs)
For a child class to be commpatible, PHP requires that all arguments (including optional ones) are defined in the function signature when a method is overridden*
Thankfully, your implementation of the function is already compatible, as you always pass all the arguments to the parent. This means the solution is nice and simple: just change line 131 in your class to
public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
and you should be good to go.
* Earlier versions of PHP raised either warnings or strict standards notices about this, but it was changed to a fatal error in PHP 8. See https://3v4l.org/uJYG1
Solution 3
Got the same error, for me it helped to replace
public function query(string $statement)
with
public function runQuery(string $statement)
I got the solution in a hint here:
Due to PHP 8.0's signature checks on LSP, DatabaseConnection::query method was renamed to DatabaseConnection::runQuery. All database drivers will now need to rename to this method for PHP 8.0 compatibility.
Harry
Updated on June 13, 2022Comments
-
Harry about 2 years
I just installed PHP 8 and I have this error appears? How do I fix it?
Fatal error: Declaration of OM\Db::query(string $statement) must be compatible with PDO::query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) in /home/www/includes/OM/Db.php on line 131
My OM/Db.php
public function query(string $statement) =====> line 131 { $statement = $this->autoPrefixTables($statement); $args = func_get_args(); if (count($args) > 1) { $DbStatement = call_user_func_array(array($this, 'parent::query'), $args); } else { $DbStatement = parent::query($statement); } if ($DbStatement !== false) { $DbStatement->setQueryCall('query'); $DbStatement->setPDO($this); } return $DbStatement; }