'Declaration of OM\Db::query(string $statement) must be compatible with PDO::query

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;
}


Solution 1:[1]

  1. Remove "doctrine/dbal": "^2.10" from composer.json

  2. Then run composer upgrade

  3. Finally run composer update

  4. Voila ! The error should be fixed !

Solution 2:[2]

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.

Solution 3:[3]

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

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Mahdi Bashirpour
Solution 2 tusch001
Solution 3 iainn