Results of 1 - 1 of about 1 for MoreErrorCode (0.001 sec.)
- 列挙型が継承できない理由 | PHP 8.3 9414
- 列挙型が継承できない理由 クラスは、メソッドの使い方を約束(契約)するものです: <?php class A {} class B extends A {} function foo ( A $a ) {} function bar ( B $b ) { foo ( $b ); } 上のコードは型安全といえます。なぜなら、 B は A との契約を守っ
... としましょう: <?php // 列挙型が final でなかったと仮定した、思考実験のコード // このコードは実際には動作しないので注意 enum MoreErrorCode extends ErrorCode { case PEBKAC ; } function fot ( MoreErrorCode $errorCode ) { quux ( $errorCode ); } fot ( MoreErrorCode ... クを通過します。 上のコードの問題点は、 quux() 関数の match 式が全ての case をカバーしてはいないということです。 なぜなら、 MoreErrorCode::PEBKAC がカバーされていないので、match 式が例外をスローするからです。 こうした理由から、列挙型は final 扱いであり、継承で ...
-
https://man.mtool.work/php/8.3/language.enumerations.object-differences.inherita...
PREV
1
NEXT