TL;DR:
- 在PHP 7.0下,不要将函数的返回值声明为null或者void,在PHP7.1下可以。
- 目前函数返回值不可被声明为nullable。
引子
今天有开发同学遇到了定义返回值为null的函数无法被load的问题:
Cannot use ‘App\null’ as class name as it is reserved
追查发现,是因为函数的返回值被声明为了null。
追查
首先,看看返回值到底能不能声明为null:
在RFC PHP RFC: Return Type Declarations中的Future Work中说:
Allow functions to declare that they do not return anything at all (void in Java and C)
这说明此RFC并不支持声明返回值为null或者void!
但是 PHP RFC: Void Return Type则表示支持void表示没有任何返回值,但是不允许用null来声明!(还给了原因)
但 此RFC会在PHP 7.1中实现,所以目前在PHP 7.0中还不能使用。
PS:经过实验,PHP7.0中,声明返回值为void可以通过语法检查,但是无论怎么返回都会报错(无论是直接return还是不返回):
Return value of App\User::func() must be an instance of void, none returned
更远一点,关于声明返回值为nullable的问题,现在还在草稿阶段,还有很长的路要走: PHP RFC: Declaring Nullable Types。
PS
在 RFC:Return Type Declarations中的Future Work中有:
Improve runtime performance by doing type analysis.
所以目前声明类型更多的是考虑方便开发,借此提升性能 仅仅是在下一步计划中而已。

Laravel simplifies handling temporary session data using its intuitive flash methods. This is perfect for displaying brief messages, alerts, or notifications within your application. Data persists only for the subsequent request by default: $request-

PHP logging is essential for monitoring and debugging web applications, as well as capturing critical events, errors, and runtime behavior. It provides valuable insights into system performance, helps identify issues, and supports faster troubleshoot

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

Laravel provides concise HTTP response simulation syntax, simplifying HTTP interaction testing. This approach significantly reduces code redundancy while making your test simulation more intuitive. The basic implementation provides a variety of response type shortcuts: use Illuminate\Support\Facades\Http; Http::fake([ 'google.com' => 'Hello World', 'github.com' => ['foo' => 'bar'], 'forge.laravel.com' =>

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Alipay PHP...

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment