Home > Blockchain >  must be compatible with PDOStatement::fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args)
must be compatible with PDOStatement::fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args)

Time:12-08

Our client has a drupal website, but the host has forced all clients from PHP 7.4 to PHP version 8, causing a PDO fatal error preventing the site from loading.

Error Received:

Fatal error: Declaration of Drupal\Core\Database\Statement::fetchAll(int $mode = PDO::FETCH_DEFAULT, $column_index = null, $constructor_arguments = null) must be compatible with PDOStatement::fetchAll(int $mode = PDO::FETCH_DEFAULT, mixed ...$args) in /usr/www/users/kdpsipxqzt/core/lib/Drupal/Core/Database/Statement.php on line 168

Function causing the problem:

ERROR (L 168) -> 
public function fetchAll($mode = null, $column_index = NULL, $constructor_arguments = NULL) {
    // Call \PDOStatement::fetchAll to fetch all rows.
    // \PDOStatement is picky about the number of arguments in some cases so we
    // need to be pass the exact number of arguments we where given.

    switch (func_num_args()) {
      case 0:
        return parent::fetchAll();
      case 1:
        return parent::fetchAll($mode);
      case 2:
        return parent::fetchAll($mode, $column_index);
      case 3:
      default:
        return parent::fetchAll($mode, $column_index, $constructor_arguments);
    }
  }

Does anyone have any ideas on resolving this?

I've tried troubleshooting in a number of ways, adjusting the function to better match the PDOStatement parent function of PHP but with no luck!

Comparing against PHP's PDOStatement:

public function fetchAll($how = null, $className = null, $ctorArgs = null)

Am I missing something?

CodePudding user response:

The return type must be set to array :

public function fetchAll($mode = null, $column_index = null, $constructor_arguments = null) : array { ... }

If you're using PHP8.1 you can bypass the error by adding the comment #[\ReturnTypeWillChange]

CodePudding user response:

As i noted in my comment above, i would not change the drupal method manually.

But here you can see, how it can be made.

public function fetchAll($mode = null, ...$args): array {
    switch (true) {
      case func_num_args()===0:
        return parent::fetchAll();
      case count($args)===0:
        return parent::fetchAll($mode);
      default:
        return parent::fetchAll($mode, ...$args);
    }
}
  • Related