TechnicalEasy PHP & Laravel

Which modern PHP 8 features do you use?

Asked for Backend Developer

Why they ask this: Checks that your PHP knowledge is current and not stuck in PHP 5-era habits.

Answer

  • Typed properties, union types and return types: public function find(int $id): ?User.
  • Constructor property promotion: public function __construct(private UserRepository $users) {}.
  • Named arguments: str_pad(string: $code, length: 6, pad_string: '0', pad_type: STR_PAD_LEFT).
  • match expression: strict comparison and returns a value, unlike switch.
  • Nullsafe operator: $user?->profile?->phone.
  • Enums (8.1): enum Status: string { case Active = 'active'; }.
  • readonly properties (8.1) and readonly classes (8.2) for immutable value objects and DTOs.
  • First-class callable syntax (8.1): array_map($this->format(...), $rows).
  • Property hooks (8.4): get/set logic declared on a property.
php
enum JobType: string
{
    case FullTime = 'full_time';
    case Internship = 'internship';

    public function label(): string
    {
        return match ($this) {
            self::FullTime => 'Full time',
            self::Internship => 'Internship',
        };
    }
}

Can you explain this in your own words?

Back to question bank