ReturnTypeWillChange
バージョンアップに伴って型宣言が変更されたインターフェイスの警告を抑止できる
PHP 8.1で追加された
どのようなときに使うのか
code:php
<?php
class MyCountable implements Countable
{
public function count()
{
return 1;
}
}
Deprecated: Return type of MyCountable::count() should either be compatible with Countable::count(): int, or the #ReturnTypeWillChange attribute should be used to temporarily suppress the notice in /in/l0JkQ on line 5 このケースにおいてはPHP 7以降だけをサポートするなら型宣言を追加するのがよい
public function count(): int
しかしPHP 5では型宣言はできない
そのようなときに#[\ReturnTypeWillChange]アトリビュートを使うとPHP 8.1は警告を発生しない
code:php
class MyCountable implements Countable
{
public function count()
PHP 8.0であっても実際にアトリビュートを取得しない限りは未定義でも問題ない
code:php
public gc(int $max_lifetime): int|false