php8.1 エラー インターフェイスの判定が厳密になった?
Tag:php
php8 → php8.1へアップデートした所、下記のようなエラーが起きました。
Fatal error: During inheritance of SessionHandlerInterface: Uncaught ErrorException: Return type of MySQLSessionHandler::open($save_path, $session_id) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in…
セッションデータをMySQLで管理するためSessionHandlerInterfaceを元に独自のクラスMySQLSessionHandlerを作っており、この部分に関するエラーのようです。
1 2 3 |
class MySQLSessionHandler implements SessionHandlerInterface{ ... } |
エラーの内容としては
SessionHandlerInterfaceを継承して作成されたMySQLSessionHandlerのopenメソッドの戻り値はboolでないとダメ!
ということなのですが、下記のように正しくboolを返すようになっています。
1 2 3 |
public function open($save_path, $session_id){ return true; } |
この場合、php7から使用できる「返り値の型指定」を行う必要があるようです。
1 2 3 |
public function open($save_path, $session_id) : bool { return true; } |
参考ページ stackoverflow
追加 2022-04-26
エラーを黙認したい場合、#[\ReturnTypeWillChange]アトリビュートを使用すれば良いようです。
1 2 3 4 |
class MyCountable implements Countable { #[\ReturnTypeWillChange] public function count() |