Typed Properties Coming to PHP 7.4


September 28th, 2018

Typed Properties Coming to PHP 7.4

The Typed Properties 2.0 RFC was accepted with a vote of 70 in favor and one no vote. A 2/3 majority is required because typed properties is a language change. The typed property change is a PHP 7.4 proposal.

With the introduction of scalar types and return types, PHP 7 greatly increased the power of PHP’s type system. However, it is currently not possible to declare types for class properties, forcing developers to use getter and setter methods instead to enforce type contracts. This requires unnecessary boilerplate, makes usage less ergonomic and hurts performance. This RFC resolves this issue by introducing support for first-class property type declarations.

The RFC provides an example where the goal is to enforce type-safety:

1class User {
2 /** @var int $id */
3 private $id;
4 /** @var string $name */
5 private $name;
7 public function __construct(int $id, string $name) {
8 $this->id = $id;
9 $this->name = $name;
10 }
12 public function getId(): int {
13 return $this->id;
14 }
15 public function setId(int $id): void {
16 $this->id = $id;
17 }
19 public function getName(): string {
20 return $this->name;
21 }
22 public function setName(string $name): void {
23 $this->name = $name;
24 }

With the new accepted RFC, that code could be functionally equivalent as follows “without sacrificing type safety”:

1class User {
2 public int $id;
3 public string $name;
5 public function __construct(int $id, string $name) {
6 $this->id = $id;
7 $this->name = $name;
8 }

Finally, here’s an example of all the valid types supported in runtime-enforced annotations for typed properties:

1class Example {
2 // All types with the exception of "void" and "callable" are supported
3 public int $scalarType;
4 protected ClassName $classType;
5 private ?ClassName $nullableClassType;
7 // Types are also legal on static properties
8 public static iterable $staticProp;
10 // Types can also be used with the "var" notation
11 var bool $flag;
13 // Typed properties may have default values (more below)
14 public string $str = "foo";
15 public ?string $nullableStr = null;
17 // The type applies to all properties in one declaration
18 public float $x, $y;
19 // equivalent to:
20 public float $x;
21 public float $y;

It’s clear from the massive 70 votes in favor to 1 vote in opposition that the PHP internals team wants to continue to introduce type features, such as type-safety, into the PHP language.

On the other end of the spectrum, PHP is usable in a completely different paradigm of a fully dynamic language embracing type coercion. However, with the continuation of new type-safety features introduced in PHP 7, it’s unclear how practical it will remain to use a fully dynamic approach to writing PHP applications.

For example, if common libraries like the Symfony components and the de-facto PHP testing framework PHPUnit implement this style of syntax, package consumers working with these libraries are required to follow suit and adapt to the new world that is a very type-focused language.

Some may argue that forcing a type-focused approach is a good thing, and will make programmers write better programs with fewer bugs. Along with this argument, static analysis tools may very well be able to detect runtime errors ahead of time.

At least for me, this type of discussion has rarely been around concrete evidence that type-safety makes one’s code better, and more around hypothetical scenarios that make it hard to discern if type-safety makes code “better.”

I don’t know if the division of programming styles will widen between dynamic and strongly typed programmers. To me, it seems inevitable that the language features will eschew dynamic programming styles due to the rising preference of developers wanting a strongly typed language.

What does that mean for those wanting to program without types? Only time will tell, but I predict that at least a small majority of programmers wishing the language to stay with its dynamic roots will look to other languages.

Learn More

To learn more the rfc:typed_properties_v2 proposal is the best source of accurate information at this point. An implementation is already under way for typed properties.

Filed in:

Paul Redmond

Full stack web developer. Author of Lumen Programming Guide and Docker for PHP Developers.