// PHP public function show() { return 'PHP 8.3: Union Types và Named Arguments'; }

Union Types

Union Types cho phép một parameter hoặc return type có thể là một trong nhiều kiểu dữ liệu:

function processValue(string|int $value): string|int
{
    if (is_string($value)) {
        return strtoupper($value);
    }
    return $value * 2;
}

Named Arguments

Named arguments cho phép bạn truyền arguments theo tên thay vì theo thứ tự:

function createUser(
    string $name,
    string $email,
    bool $isAdmin = false,
    ?string $avatar = null
) {
    // ...
}

// Sử dụng named arguments
createUser(
    name: 'John Doe',
    email: 'john@example.com',
    isAdmin: true
);

Lợi ích

  • Code rõ ràng hơn
  • Dễ bảo trì hơn
  • Giảm lỗi khi thay đổi thứ tự parameters

Kết luận

PHP 8.3 tiếp tục cải thiện ngôn ngữ với các tính năng hiện đại, giúp developer viết code tốt hơn.