3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface HasMethodReturnIntOrString { public function methodA(): int|string; } interface HasMethodReturnIntOrFloat { public function methodA(): int|float; } /** * 返り値が int|string 型であるメソッド methodA の実装を約束するインターフェイスと * 返り値が int|float 型であるメソッド methodA の実装を約束するインターフェイスの * 両方を実装するクラス */ class HasMethodReturnInt implements HasMethodReturnIntOrString, HasMethodReturnIntOrFloat { /** * 返り値が int|string 型 と int|float 型の両方を満たすメソッド * つまり (int|string) & (int|float) の計算結果である int 型の値を返すメソッド * @return int */ public function methodA(): int { return 0; } // 片方の型を満たせない場合↓の様にPHPがエラーを出力します // Fatal error: Declaration of HasMethodReturnInt::methodA(): string must be compatible with HasMethodReturnStringOrFloat::methodA(): int|float in ***\tmp.php on line 28 // public function methodA(): string|int // { // return 0; // } }
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
Output for 7.4.0 - 7.4.33
Parse error: syntax error, unexpected '|', expecting ';' or '{' in /in/9MjGI on line 5
Process exited with code 255.

preferences:
113.12 ms | 401 KiB | 121 Q