must be empty when class provided in argument #2 ($class) does not have a constructor

Description

The class referenced in the PDO::fetchAll method does not have a constructor. No argument will be handed to it, so there is no need to pass that argument.

Example

<?php

class X
{
    public $id;
    protected $val;
    private $val2;
}

$stmt->execute(); // STMT is a PDO statement
try {
    var_dump($stmt->fetchAll(PDO::FETCH_CLASS, X::class, [0]));
} catch (\Throwable $e) {
    echo $e::class, ': ', $e->getMessage(), \PHP_EOL;
}

?>

Solutions

  • Omit the third argument.

  • Use an empty array for the third argument.

  • Use a null value for the third argument.

Changed Behavior

This error may appear following an evolution in behavior, in previous versions. See ` <https://php-changed-behaviors.readthedocs.io/en/latest/behavior/.html>`_.