用下面的代码PHP 7.1
class MySqlSessionHandler implements \SessionHandlerInterface {
public function read(string $session_id) : string { ... }
}
下,我得到:
Fatal error: Declaration of Sessions\MySqlSessionHandler::read(string $session_id): string must be compatible with SessionHandlerInterface::read($key)
当我删除类型声明“串”,而不是像
public function read($session_id) : string { ... }
不会抛出异常。所有其他的SessionHandlerInterface函数也是如此。阅读PHP文档类型字符串应该是正确的。
出了什么问题?
删除这实际上不是一个重复的相关问题。该问题在用户定义的接口和类之间不匹配,而这个问题是由[PHP中的错误](https://bugs.php.net/bug.php?id=72285)造成的。官方手册中记录的方法签名是错误的;此时,在实现'SessionHanderInterface'时不能使用类型声明。 – Zilk