Gelişmiş Tip Sistemi
PHP 7+ ve 8+ ile gelen güçlü tip sistemi: tip bildirimleri, birleşim (union) tipleri, nullsafe operatör, readonly, enum'lar ve tip dönüşümlerinin performans etkileri. Tip güvenliği, runtime kontroller ve phpdoc ile generics benzeri yapıların kullanımı ele alınır.
Konular: strict types, union/intersection, nullable, readonly, enums, tip dönüşümleri, phpdoc generics.
union ve readonly örneği
<?php
declare(strict_types=1);
enum Status: string { case ACTIVE = 'active'; case INACTIVE = 'inactive'; }
class User {
public readonly int $id;
public function __construct(int $id, public Status $status) {
$this->id = $id;
}
}
function parseId(int|string $value): int {
return is_int($value) ? $value : (int)$value;
}
$user = new User(parseId("42"), Status::ACTIVE);
echo $user->status->value;
?>