Typed properties in PHP 7.4
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
orprivate
; orvar
. - All types are allowed, except
void
andcallable
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.