Declaration of OM\Db::query(string $statement) must be compatible with PDO::query
Upgrading to PHP 8 triggers a specific fatal error when extending the PDO class: Fatal error: Declaration of OM\Db::query(string $statement) must be compatible with PDO::query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs). This happens because the method signature in your custom OM\Db class no longer matches the new requirements imposed by PHP 8.0+ regarding variadic arguments and optional parameters.
The root cause lies in how PDO::query was updated for PHP 8. The native implementation now accepts an optional second argument, $fetchMode, followed by a variable number of additional arguments ($fetchModeArgs) that define specific fetch modes. When you declare your overridden method with only a single required string parameter, the PHP runtime detects an incompatibility and halts execution to prevent unexpected behavior changes in inherited functionality.
To resolve this, you must align the signature of OM\Db::query with the parent class. The updated implementation needs to accept the optional $fetchMode and the variadic arguments. Here is how the corrected method should look:
public function query(string $statement, ?int $fetchMode = null, mixed ...$fetchModeArgs)
{
// Apply your custom logic here first
$statement = $this->autoPrefixTables($statement);
// Pass all arguments to the parent class to ensure compatibility
$DbStatement = parent::query($statement, $fetchMode, ...$fetchModeArgs);
if ($DbStatement !== false) {
$DbStatement->setQueryCall('query');
$DbStatement->setPDO($this);
}
return $DbStatement;
}
The critical change is the addition of ?int $fetchMode = null and mixed ...$fetchModeArgs. The question mark indicates that the fetch mode is optional, allowing calls like $pdo->query('SELECT * FROM users') to continue working without arguments. The ellipsis (...) captures any additional arguments passed to the method, which are then forwarded to the parent class using the splat operator (...).
If your original code used func_get_args() to capture all arguments dynamically, you should replace that pattern with explicit parameter definitions followed by passing them directly to parent::query(). This approach is more robust and aligns with modern PHP practices. Using call_user_func_array or func_get_args inside an overridden method can sometimes obscure the actual argument flow and make debugging harder, especially when dealing with variadic parameters in newer PHP versions.
One important consideration is that once you accept the optional $fetchMode, your code must handle cases where it is null. The parent class logic expects to receive these arguments to determine how the result set should be returned (e.g., as an associative array, a single value, or columns). By forwarding them correctly to parent::query(), you ensure that any fetch mode specified in the outer call is respected within your wrapper.
If you are maintaining legacy code where $fetchMode was never intended to be used, changing the signature might still feel intrusive. However, ignoring the error is not an option; PHP 8 requires strict signature compatibility. You could consider suppressing the error temporarily with @ if you were in a controlled environment, but that is generally discouraged as it masks potential logic errors. The cleanest solution remains updating the method definition to match the parent class.
For teams using Laravel or similar frameworks, this issue highlights the importance of checking parent class changes when upgrading PHP versions. Tools like PHPCS or static analyzers can help catch signature mismatches before they become fatal errors in production. If you are looking for additional resources on handling PDO compatibility in custom wrappers, check out https://laravelcompany.com for insights on maintaining robust database abstraction layers.
By updating the method signature and ensuring all arguments are properly forwarded, your OM\Db wrapper will function correctly under PHP 8 while preserving the ability to fetch results in various modes if needed.