【问题标题】:PHP 7: SessionHandlerInterface::read(string $session_id): string must be compatible with SessionHandlerInterface::read($key) [duplicate]PHP 7:SessionHandlerInterface::read(string $session_id):字符串必须与 SessionHandlerInterface::read($key) 兼容 [重复]
【发布时间】:2017-09-21 06:11:45
【问题描述】:

在 PHP 7.1 下使用以下代码

class MySqlSessionHandler implements \SessionHandlerInterface   {
     public function read(string $session_id) : string  { ... }
}

我明白了:

致命错误:Sessions\MySqlSessionHandler::read(string 声明 $session_id): 字符串必须兼容 SessionHandlerInterface::read($key)

当我删除类型声明“字符串”而不是像

public function read($session_id) : string  { ... }

不会抛出异常。所有其他 SessionHandlerInterface 函数也是如此。阅读 PHP 文档类型字符串应该是正确的。

怎么了?

【问题讨论】:

  • 这实际上不是链接问题的副本。该问题在用户定义的接口和类之间存在不匹配,而此问题是由bug in PHP 引起的。官方手册中记录的方法签名是错误的;此时,实现SessionHanderInterface时不能使用类型声明。

标签: php session php-7.1


【解决方案1】:

函数的原始签名不包含输入,因此当您添加“字符串”时,它是不同的签名,因此您可以将“字符串”添加到接口签名或从实现中删除

【讨论】:

  • 我明白了 - 感谢您的快速回答。我将从我的实施中删除
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-02-01
相关资源
最近更新 更多