PHP 7.4: Typed Properties

Preface

With PHP 7.4 upcoming, it’s time to start exploring some of the new features that will be arriving alongside it. In this article we look into the new addition of Typed Properties, this is where properties on objects can be type cast instead. This is a key change to help push PHP along on the type path, enforcing things such as type-safety.

Basics

The best example for this is the example that’s actually provided within the RFC itself. The below chunk of code is what you might need pre-7.4 to ensure that the values that you’re passing in etc match the type of the properties.

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

This can be simply rewritten as:

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

The reason a lot of this can be simplified, is because in the original code, you had to use the setId or setName methods to ensure that the values passed in were of the correct type. In PHP 7.4, you can now go directly to the parameter and PHP itself will ensure that the types of the values you assign to the properties match those the class defintion requires.