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文件中声明一个类呢?可以使 ...
随机推荐
- openssl 编译
不要费事编译了,直接下载吧! https://www.npcglib.org/~stathis/blog/precompiled-openssl/ 下载 openssl https://www.ope ...
- Hadoop资源调度器
hadoop调度器的作用是将系统中空闲的资源按一定策略分配给作业.调度器是一个可插拔的模块,用户可以根据自己的实际应用要求设计调度器.Hadoop中常见的调度器有三种,分别为: 1.基于队列的FIFO ...
- ifconfig设置ip时出现提示 ifconfig: SIOCSIFFLAGS: Address not available
一.笔者使用ifconfig观察网卡情况如下: root@jello:/# ifconfig eth0 eth0 Link encap:Ethernet HWaddr 00:00:00:00:00:0 ...
- RMI远程方法调用
RMI远程方法调用:适用于 客户端 调用 服务器 内的方法:(Kotlin 语言编写) 如果业务为二个服务器之间的通信,还是得用消息队列的形式,因为RMI 不适合 双向 调用 下面介绍RMI 的使用方 ...
- SaltStack部署配置Tomcat-第三篇
实验目标 简单部署tomcat及安装java环境 实现步骤 编写salt的状态模块 [root@linux-node1 web]# pwd /srv/salt/base/web [root@linux ...
- mysql索引的建立和使用
转自[http://www.cnblogs.com/mywebname/articles/555696.html] 一.索引的概念 索引就是加快检索表中数据的方法.数据库的索引类似于书籍 ...
- Animal_human_kp人脸与马脸迁移学习GitHub 论文实现
Interspecies Knowledge Transfer for Facial Keypoint Detection关键点检测 Github地址:Interspecies Knowledge ...
- apt get update无法正常使用解决方案(转载)
apt get update无法正常使用 解决方法参考博客 [问题描述] 前几天执行apt相关命令(如apt-get update),都会长时间停在``等待报头'',超时后,显示连接超时. 换了快速指 ...
- Kubernetes容器运行时(CRI)简介
Kubernetes节点的底层由一个叫做“容器运行时”的软件进行支撑,它负责比如启停容器这样的事情.最广为人知的容器运行时当属Docker,但它不是唯一的.事实上,容器运行时这个领域发展迅速.为了使K ...
- 【Python】更优的字符串格式化方式 -- "format"替代"%s"
背景 前段时间看了一篇介绍Python的代码技巧的文章,建议格式化字符串时使用"format"代替使用"%",但是没有说明原因.各博客网站介绍相关用法的博客很多 ...