sysuser
PREMİUM
- Katılım
- 9 Kas 2025
- Mesajlar
- 174
- Tepki puanı
- 4
- Cinsiyet
- Kadın
PHP'de Union Type ve Intersection Type Kullanımı
PHP 8.0 union type, 8.1 intersection type desteği getirerek tip sistemi ifade gücünü önemli ölçüde artırdı.
Union Type
PHP:
function format(int|float $value): string {
return number_format($value, 2);
}
Intersection Type
PHP:
function process(Countable&Iterator $collection): void {
// Hem Countable hem Iterator olmalı
}
DNF Type (PHP 8.2)
PHP:
function handle((Stringable&JsonSerializable)|null $obj): void { ... }
Kullanım Rehberi
- Union type: farklı türlerde olabilecek parametreler için
- Intersection type: birden fazla interface gerektiren bağımlılıklar için
- null ile union yerine soru işareti kısaltması tercih edin: ?string