Property Type Declaration

Type declaration property, also known as typed properties, is a feature introduced in PHP 7.4 that allows to specify the type of a class property.

With type declaration properties, the type of a property is explicitly defined, ensuring that only values of the specified type can be assigned to that property. This helps enforce type safety and prevents accidental assignments of incompatible values.

Typed properties support any type format : simple, union, intersectional or DNF.

<?php

class x {
    private Typed $y;
}

?>

Documentation

Related : Type System, Union Type, Intersection Type, Disjunctive Normal Form (DNF)

Added in PHP 7.4