In PHP7.0, the return type of the function cannot be defined as null or void TL; DR:
- In PHP 7.0, do not declare the return value of a function as null or void. in PHP7.1, you can.
- Currently, the function return value cannot be declared as nullable.
Introduction
Today, some developers have encountered the problem that a function that defines the return value as null cannot be loaded:
Cannot use 'app \ Null' as class name as it is reserved
Tracing finds that the return value of the function is declared as null.
Tracing
First, check whether the returned value can be declared as null:
The Future Work in rfc php rfc: Return Type Declarations says:
Allow functions to declare that they do not return anything at all (void in Java and C)
This indicates that this RFC does not support declaring that the return value is null or void!
However, php rfc: Void Return Type indicates that void is supported, indicating that no Return value is returned, but null is not allowed! (The reason is also returned)
HoweverThis RFC will be implemented in PHP 7.1So it is not available in PHP 7.0 currently.
PS: After the experiment, you can run the syntax check to declare the return value as void in PHP7.0, but an error will be returned no matter how it is returned (whether it is direct return or not ):
Return value of App \ User: func () must be an instance of void, none returned
Further, there is still a long way to go about Declaring the return value as nullable in the draft phase: php rfc: Declaring Nullable Types.
PS
The Future Work in RFC: Return Type Declarations includes:
Improve runtime performance by doing type analysis.
Currently, more types of declarations are considered for convenient development to improve performance.Just in the next plan.