Nullable Type Usage Analysis for new features of PHP7.1, php7.1nullable
This article analyzes the Nullable Type usage of the new function PHP7.1. We will share this with you for your reference. The details are as follows:
In the PHP5 era, PHP parameters already support type hint (except the basic type), so we should be familiar with it. Later, the PHP7 era came, PHP can also specify the type of the returned value (and the type hint of the basic type ). However, we may have another requirement: in addition to the specified type hint, the parameter or return value can also be defined as null. For example, if we have a UserRepository class, there is a method called find ($ id). This method can return a User object or return null, which can be defined in PHP7.1:
...
class UserRepository{ ... public function find($id): ?User { ... return $user; // or // return null; }}
However, after such a statement, if null is returned, whether or not return null must be explicitly called must be verified after 7.1 is officially released.
Of course, there is also an example of whether to set nullable. Copy the example on the official website:
Function say (? String $ msg) {if ($ msg) {echo $ msg;} say ('hello'); // OK -- prints hellosay (null ); // OK -- does not printsay (); // error -- loss attribute say (new stdclass); // error -- Type error
The above is the general usage of nullable type, but after this mechanism is introduced, many details need to be paid attention to, for example, during inheritance, subclass allows you to remove the setting that the returned type of the method with the same name as the parent class is nullable:
interface Fooable{ public function foo(): ?Fooable;}interface StrictFooable extends Fooable{ public function foo(): Fooable; // valid}
However, the subclass does not allow the nullable type to be added.
Then let's take a look at the parameter type. In contrast to the return type, when a subclass rewrites the parent class method, the parameter type can be added based on the parent class parameter:
interface Fooable{ public function foo(Fooable $f);}interface LooseFoo extends Fooable{ function foo(?Fooable $f);}
This is not the case. We may be wondering why the returned type and parameter type have such settings. As a matter of fact, you can understand the Liskov replacement principle.
The last question is about the default parameter values. Currently, PHP can do this:
function foo_default(Bar $bar = null) {}foo_default(new Bar); // validfoo_default(null); // validfoo_default(); // valid
However, if it is changed to the nullable type declaration, even if the input parameter is null, it cannot be omitted during the call:
function foo_nullable(?Bar $bar) {}foo_nullable(new Bar); // validfoo_nullable(null); // validfoo_nullable(); // INVALID!
It can be seen that the nullable type is designed to allow a parameter with a type and can be null, while the default value of a parameter is null. In the past, if we allowed some setters to be empty, the code we wrote was always strange:
public function setIssueAt(\DateTime $issueAt = null)
I think the biggest reason is that such a setter can not pass parameters!
Now, with nullable type, we will not write such awkward code.
Of course, nullable type and = null can be used at the same time. Here we will not write an example. The effect is no different from that of = null.
Finally, inherit: if the method parameter of the parent class is nullable, The subclass can use = null.
interface Contract{ public function method(?Foo $foo): bool;}class Implementation implements Contract{ public function method(?Foo $foo = null): bool { return is_null($foo); }}