Return type declarations返回类型声明
PHP 7.新增了返回类型声明
http://php.net/manual/en/functions.returning-values.php
在PHP 7.1中新增了返回类型声明为void,以及类型前面增加问号表示可以返回null,例如?init,不过和某些语言中的细节略有不同,没什么技术含量,就是语言的定义,直接上代码吧。
php -r "function a() : int {return 1;} var_dump(a());"
int(1)
php -r "function b() : int {return;} var_dump(b());"
Fatal error: A function with return type must return a value in Command line code on line 1
php -r "function c() : int {return null;} var_dump(c());"
Fatal error: Uncaught TypeError: Return value of c() must be of the type integer, null returned in Command line code:1
php -r "function d() : null {return 1;} var_dump(d());"
Fatal error: Cannot use 'null' as class name as it is reserved in Command line code on line 1
php -r "function e() : void {return 1;} var_dump(e());"
Fatal error: A void function must not return a value in Command line code on line 1
php -r "function f() : void {return;} var_dump(f());"
NULL
php -r "function g() : void {return null;} var_dump(g());"
Fatal error: A void function must not return a value (did you mean "return;" instead of "return null;"?) in Command line code on line 1
php -r "function h() : ?null {return 1;} var_dump(h());"
Fatal error: Cannot use 'null' as class name as it is reserved in Command line code on line 1
php -r "function i() : ?void {return 1;} var_dump(i());"
Fatal error: Void type cannot be nullable in Command line code on line 1
php -r "function j() : ?int {return 1;} var_dump(j());"
int(1)
php -r "function k() : ?int {return;} var_dump(k());"
Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?) in Command line code on line 1
php -r "function l() : ?int {return null;} var_dump(l());"
NULL
php -r "function m() : void {return;} function n() : ?int {return null;} var_dump(m() === n());"
bool(true)
Return type declarations返回类型声明的更多相关文章
- C++ 函数模板的返回类型如何确定?
函数模板 #include <iostream> // 多个参数的函数木板 template<typename T1, typename T2> T2 max(T1 a, T2 ...
- PHP 标量类型与返回值类型声明
标量类型声明 默认情况下,所有的PHP文件都处于弱类型校验模式. PHP 7 增加了标量类型声明的特性,标量类型声明有两种模式: 强制模式 (默认) 严格模式 标量类型声明语法格式: declare( ...
- item 5: 比起显式的类型声明,更偏爱auto
本文翻译自modern effective C++,由于水平有限,故无法保证翻译完全正确,欢迎指出错误.谢谢! 博客已经迁移到这里啦 啊,简单愉快的代码: int x; 等等,讨厌!我忘了初始化x,所 ...
- scala学习手记20 - 方法返回类型推断
除了推演变量的类型,scala也会推演方法的返回类型.不过这里有一处需要注意:方法返回类型的推演依赖于方法的定义方式.如果用等号"="定义方法,scala就会推演方法返回类型:否则 ...
- Go语言规格说明书 之 类型声明(Type declarations)
go version go1.11 windows/amd64 本文为阅读Go语言中文官网的规则说明书(https://golang.google.cn/ref/spec)而做的笔记,完整的介绍Go语 ...
- [Effective Modern C++] Item 5. Prefer auto to explicit type declarations - 相对显式类型声明,更倾向使用auto
条款5 相对显式类型声明,更倾向使用auto 基础知识 auto能大大方便变量的定义,可以表示仅由编译器知道的类型. template<typename It> void dwim(It ...
- c++11: trailing return type in functions(函数返回类型后置)
In C++03, the return type of a function template cannot be generalized if the return type relies on ...
- 【c++错误】类的语法错误 error c2533:constructors not allowed a return type(构造函数不允许返回一个类型)
今天编写类的程序的时候不小心把类后的分号忘写了,就出现上面的错误提示. 顺便复习下类的正确格式: class 类名 { public: //习惯上将公有类型放在前面,便于阅读 ……(外部接口) pro ...
- drools的类型声明(Type declarations)
一.背景 在我们编写drl规则的时候,有些时候需要自己声明一些类,用于辅助之后的规则运行,如果需要用到的类还需要在java中预先声明出来,这样就不灵活了,那么是否可以在drl文件中声明一个类呢?可以使 ...
随机推荐
- Jclemo_ CTF_WEEK1~2学习总结
Jclemo_ CTF_WEEK1~2学习总结 纯属因为感觉有趣,加入了ForDKYCTF小组学习,心得体会就不说了,总结一下最近的学习知识点(不全,参考我自己的940133658@qq.com的印象 ...
- 20145324 《Java程序设计》第10周学习总结
20145324 <Java程序设计>第10周学习总结 教材学习内容总结 1.网络编程的实质就是两个(或多个)设备(例如计算机)之间的数据传输 2.在实际传输数据以前需要将域名转换为IP地 ...
- Eye Protection FAQ
Q: Why does smart protection not work? A: Please make sure the checkbox "Eye Protection" i ...
- CORE MVC 自定义认证
微软的认证体系,集成了EF,和它的表结构,对于我们已有的系统,或者想高度自定义的人,该怎么办呢? 答案在: https://docs.microsoft.com/en-us/aspnet/core/s ...
- windows使用Pandoc将Markdown转换为PDF文件
pandoc下载:https://github.com/jgm/pandoc/releases/tag/1.19.2.1 //windows下载msi文件 miktex下载:https://mikte ...
- LeetCode——maximal-rectangle
Question Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing all ...
- Hive中的数据倾斜
Hive中的数据倾斜 hive 1. 什么是数据倾斜 mapreduce中,相同key的value都给一个reduce,如果个别key的数据过多,而其他key的较少,就会出现数据倾斜.通俗的说,就是我 ...
- [PyTorch]PyTorch中反卷积的用法
文章来源:https://www.jianshu.com/p/01577e86e506 pytorch中的 2D 卷积层 和 2D 反卷积层 函数分别如下: class torch.nn.Conv2d ...
- 从0开始 Java学习 packet用法
packet 包的用法 参考博客:https://www.cnblogs.com/Ring1981/p/6240412.html 用法 java 源文件带有包名,往往容易出错 如:H:\code\He ...
- hdu4310 - Hero - 简单的贪心
2017-08-26 15:25:22 writer:pprp 题意描述: • 1 VS n对战,回合制(你打他们一下,需要受到他们所有存活人的攻击)• 你的血量无上限,攻击力为1• 对手血量及攻击 ...