Что такое type hinting? Где используется?

Ответ

Type hinting (объявление типов) - указание ожидаемых типов для параметров функций, возвращаемых значений и свойств классов.

function greet(string $name): string {
    return "Hello, $name";
}

class User {
    public int $age;
    public ?string $email; // nullable
}

Доступные типы: скалярные (int, float, string, bool), составные (array, object), классы/интерфейсы, callable, iterable, self, static, mixed, void, never, null, true, false.

С PHP 8.0 - union types (int|string), с PHP 8.1 - intersection types (A&B), с PHP 8.2 - DNF types ((A&B)|C).

🧠Квиз 🏆Лидеры 🎯Собесед. 📖Вопросы 📚База зн.