类型化属性

版本新特性 / PHP 7.4

// *** before: ***
class User {
    /** @var int */
    public $id;
    /** @var string */
    public $name;
    // a docblock is documentation only, nothing enforces it
}

$user = new User();
$user->id = 'not an id'; // silently accepted

// *** in version 7.4: ***
class User {
    public int $id;
    public string $name;
}

$user = new User();
$user->id = 'not an id'; // TypeError: must be of type int, string given