Sign in
Log inSign up

Typed properties in PHP 7.4

Brent Roose's photo
Brent Roose
·Jun 14, 2019

Typed class properties have been added in PHP 7.4 and provide a major improvement to PHP's type system. These changes are fully opt-in and backwards compatible. Here's a very short summary of how typed properties work:

  • They are available as of PHP 7.4, which is scheduled to be released in November of 2019
  • They are only available in classes and require an access modifier: public, protected or private; or var.
  • All types are allowed, except void and callable

This is what they look like in action:

class Foo
{
    public int $a;

    public ?string $b = 1;

    private Foo $prop;

    protected static string $static = 'default';
}

There's lots more to tell about typed properties, so I wrote an in-depth post about them here: https://stitcher.io/blog/typed-properties-in-php-74.