Skip to main content
InterviewHubPRO
Section Overview
Q36

Question 36 of 50

Part 3: Production Tuning, Asynchronous Workers & High Scale (25 - 35)

72%
PHP & Zend Engine (50 Qs)•Part 3: Production Tuning, Asynchronous Workers & High Scale (25 - 35)

Modern PHP Features (PHP 8.1 - 8.4): Enums, Readonly Classes, and Property Hooks.

senior LevelCode ExampleHooksAsync & PromisesTypeScript

In Simple Words: Modern PHP has evolved into a strictly typed, expressive object-oriented language:

  • Backed Enums (PHP 8.1): Typed enumerations with scalar values (enum Status: string { case Active = 'A'; }) that can implement interfaces and contain methods.
  • Readonly Classes (PHP 8.2): Declaring readonly class makes all properties immutable once initialized.
  • First-Class Callables (PHP 8.1): Syntax strlen(...) creates closures without string references.
  • Property Hooks (PHP 8.4): Native get/set accessors on class properties, eliminating verbose getter/setter boilerplate methods!
php
// PHP 8.4 Property Hooks and Backed Enums
enum UserRole: string {
    case Admin = 'admin';
    case Customer = 'customer';

    public function canManage(): bool {
        return $this === self::Admin;
    }
}

class UserProfile {
    // Property Hook (PHP 8.4)
    public string $fullName {
        get => "{$this->firstName} {$this->lastName}";
        set (string $value) {
            [$this->firstName, $this->lastName] = explode(' ', $value, 2);
        }
    }

    public function __construct(
        public string $firstName,
        public string $lastName,
        public readonly UserRole $role
    ) {}
}