Introduction
Many languages, especially scripting languages, have a loosely typed variables that are weakly typed. In the weak typed languages, variables can hold any type of values. For example, when you assign a number to a variable, it will be treated as a number; or string when assigned a text, etc.
Typed class properties were introduced in PHP 7.4 around 2019. It has only two requirementss:
1. Typed Properties can only be in classes,
2. It must has one of the following visibility modifiers: public, protected, or private; or var.
With the exception of void and callable, all types are allowed:
* string
* int
* bool
* float
* array
* iterable
* object
* ? (nullable)
* self & parent
* Classes & interfaces
This is what they look like in action:
1 2 3 4 5 6 7 8 9 10 | class Orders { public int $id; public ?string $description protected static string $status private array $list; } |
Why typed properties?
Strong typed properties allows tools like code-analysis to help programmers in preventing runtime problems during coding, safe refactoring and application scaling.
It ensures the same data type being used through out the code, and catches at ‘mixed’ types early at ‘compile’ time, albeit PHP being a dynamic language. For example, when multiplying a string by an integer or assigning an integer to a string variable type, the type checking can help you uncover issues without running the code by doing a quick and simple static analysis.
Initialize typed properties
It’s possible to provide a default value to typed properties.
1 2 3 4 5 6 7 8 9 10 | class Orders { private int $id; public ?string $description = 'my description'; protected static string $status = 'Status'; private array $list = [1, 2, 3]; } |
It’s worth noting that it’s OK to have an uninitialized property outside of the constructor such as $id in code sample above as long as nothing it is not accessed before it is initialized (assigned with a value).
If typed values not initialized, the second obvious place to initialize would be the constructor:
1 2 3 4 5 6 7 8 9 | class Order { public int $id; public function __construct(int $id) { $this->id = $id; } } |
Strict types
Because PHP is the dynamic language, even with the typed properties, it will always try to coerce or convert types whenever possible. If you supply a string instead of an integer, PHP will attempt to convert it automatically.
The following code (with typed variables) is valid and will convert ‘1’ to 1.
1 2 3 4 5 6 7 8 | class Order { public int $id; } $order = new Order; $order->id = '9'; // converted to integer 9 |
To enforce our typed variables, declare strict types before class:
1 | declare(strict_types=1); |
It will throw a TypeError:
1 2 | Fatal error: Uncaught TypeError: Typed property Order::$id must be int, string used |
Final thought
Typed variables are a huge step forward for PHP’s future. Despite this, PHP is not a strong type programming language, and it probably never will be.
Happy coding!
You can find more details on PHP.net official typed properties.