Nullable, void, and uninitialized
There are three special types in PHP that represent no value at all. The first is null
; it is something that is expected as a valid value for your properties and return types, though it is something that you need to account for.
Another one is void
and it really does mean nothing. It is only used as a return type and it defines that the function or method does not return anything at all.
The final one is uninitialised
, which is a special limbo state that has been introduced along with PHP 8 constructor property promotion. We will explore how all of these work.
Null and nullable
First, let's have a look at null
. null
behaves like a scalar value in that you can assign it, check for it, and return it as an option on any other type.
src/Part2/Chapter4/null.php
Repo: https://git.io/JRwd0
<?php declare(strict_types=1); namespace Book\Part2\Chapter4; // assigning null as a value $foo = null; // short...