用下面的代碼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