8

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;
}
Dharman
  • 30,962
  • 25
  • 85
  • 135
Harry
  • 357
  • 3
  • 4
  • 13

3 Answers3

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

  2. Then (and finally) run composer update

hakre
  • 193,403
  • 52
  • 435
  • 836
David Raleche
  • 436
  • 5
  • 11
7

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

iainn
  • 16,826
  • 9
  • 33
  • 40
  • Hello, I tried this but it do not work. public function query ( string $statement , int $fetch_style = null, ?string $classname , ?array $ctorargs ) – Harry Nov 07 '20 at 13:12
  • Right - that's still different from the parent method signature, so you'll get the same error. It needs to be compatible with the parent, removing arguments or adding extra ones isn't allowed. If you want a method with other arguments, you'll need to give it a different name. – iainn Nov 07 '20 at 14:22
  • What is exactly the parent function ? this is not clear - public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs) - Tk – Harry Nov 07 '20 at 14:32
4

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.

hakre
  • 193,403
  • 52
  • 435
  • 836
tusch001
  • 41
  • 1