In PHP7.0, the return type of the function cannot be defined as null or void.

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.