【问题标题】:Declaration of OM\Db::query(string $statement) must be compatible with PDO::queryOM\Db::query(string $statement) 的声明必须与 PDO::query 兼容
【发布时间】:2021-02-19 21:53:18
【问题描述】:

我刚刚安装了 PHP 8 并且出现了这个错误?我该如何解决?

致命错误:OM\Db::query(string $statement) 的声明必须与 /home/ 中的 PDO::query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs) 兼容www/includes/OM/Db.php 第 131 行

我的 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;
}

【问题讨论】:

    标签: php php-8


    【解决方案1】:

    #1 - 从作曲家 Json 中删除类似的行

    "doctrine/dbal": "^2.10",

    #2 - 运行

    作曲家升级

    #3 - 运行

    作曲家更新

    #4 - 瞧!错误应该被修复!

    【讨论】:

      【解决方案2】:

      为了稍微扩展错误消息,您的类中query 函数的签名必须与 PDO 类中的父方法兼容。

      你有这个:

      public function query(string $statement)
      

      ,父类有这个:

      public function query(string $query, ?int $fetchMode = null, mixed ...$fetchModeArgs)
      

      为了使子类兼容,PHP 要求在重写方法时在函数签名中定义所有参数(包括可选参数)*

      谢天谢地,您的函数实现已经兼容,因为您总是将所有参数传递给父级。这意味着解决方案既好又简单:只需将班级中的第 131 行更改为

      public function query(string $query, ?int $fetchMode = null, ...$fetchModeArgs)
      

      你应该很高兴。

      * 早期版本的 PHP 对此提出警告或严格标准通知,但在 PHP 8 中已更改为致命错误。请参阅 https://3v4l.org/uJYG1

      【讨论】:

      • 您好,我试过了,但它不起作用。公共函数查询(字符串 $statement , int $fetch_style = null, ?string $classname , ?array $ctorargs )
      • 对 - 这仍然与父方法签名不同,所以你会得到同样的错误。它需要与父级兼容,不允许删除参数或添加额外的参数。如果你想要一个带有其他参数的方法,你需要给它一个不同的名字。
      • 父函数到底是什么?这不清楚 - 公共函数查询(字符串 $query, ?int $fetchMode = null, ...$fetchModeArgs) - Tk
      【解决方案3】:

      遇到同样的错误,对我来说它有助于替换

      公共函数查询(字符串 $statement)

      公共函数runQuery(string $statement)

      我得到了提示here的解决方案:

      由于 PHP 8.0 对 LSP 的签名检查,DatabaseConnection::query 方法被重命名为 DatabaseConnection::runQuery。所有数据库驱动程序现在都需要重命名为此方法以兼容 PHP 8.0。

      【讨论】:

        猜你喜欢
        • 2017-11-07
        • 2019-07-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-07-01
        • 1970-01-01
        • 2021-06-06
        相关资源
        最近更新 更多