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文件中声明一个类呢?可以使 ...
随机推荐
- 20145327 《Java程序设计》第四周学习总结
20145327 <Java程序设计>第四周学习总结 教材学习内容总结 继承也符合DRY原则. Java中只有单一继承,也就是只能有一个父类 继承可以复用代码,更大的用处是实现「多态」:封 ...
- localAddress
$(function(){ <% out.println("/** ip:"+request.getLocalAddr()+"("+request.get ...
- Live Score FAQ
Q: Why doesn't the selected game go to top? A: The game which include your favorite team will be alw ...
- 拓扑排序(dfs)
int c[N];//c[u]=0表示从来没有访问过:c[u]=1表示已经访问过,并且还递归访问过它的所有子:c[u]=-1表示正在访问. int topo[N],t; int G[N][N]; bo ...
- Oracle邮件推送函数
CREATE OR REPLACE PROCEDURE PROCSENDEMAIL ( P_TXT VARCHAR2, P_SUB VARCHAR2, P_SENDOR VARCHAR2, P_REC ...
- C3 文件IO:APUE 笔记
C3:文件IO 1 引言 本章描述的函数被成为不带缓冲的IO,涉及5个函数:open.read.write.lseek.close. 文件控制:dup.sync.fsync.fdatasync.fcn ...
- C++(二十六) — 构造函数、析构函数、对象数组、复制构造函数
1.构造函数 (1)每个类都要定义它自己的构造函数和析构函数,是类的成员函数. 特点:名称与类名相同:没有返回值:一定是共有函数,可以直接访问类内所有成员函数:可以带默认形参,可以重载: class ...
- JAVA常用数据结构API
Quene
- 【转载】deque双向队列
继vector和queue之后,又发现一个很好用的东西. 本篇转载自http://blog.csdn.net/morewindows/article/details/6946811 deque双向队列 ...
- IOS-Quartz2D
一.画基本图形 // // BWView.m // IOS_0221_Quartz2D画矩形 // // Created by ma c on 16/2/21. // Copyright © 2016 ...