skip to Main Content

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
DrupalCoreDatabaseStatement::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?

2

Answers


  1. 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]

    Login or Signup to reply.
  2. 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);
        }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search