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文件中声明一个类呢?可以使 ...
随机推荐
- 20145321 《Java程序设计》第2周学习总结
20145321 <Java程序设计>第2周学习总结 教材学习内容总结 一.类型.变量.运算符 1.类型(基本类型) (1)整数:short(占2字节),int(占4字节),long(占8 ...
- 尝试编辑java程序
尝试编译java程序 之前在用软件eclipse编译过简单的hello java程序,因为软件操作简单,后来学会了用命令符来编译程序.代码如下 public class abc { ...
- js 代码执行时间
<html> <head> </script> <script> var sTime=new Date().getTime(); alert(&qu ...
- 转载 - POJ分类很好很有层次感
from http://blog.csdn.net/zzycsx/article/details/49103451 OJ上的一些水题(可用来练手和增加自信) (poj3299,poj2159,poj2 ...
- AVL树 - 学习笔记
2017-08-29 14:35:55 writer:pprp AVL树就是带有平衡条件的二叉查找树.每个节点的左子树和右子树高度相差最多为1的二叉查找树 空树的高度定为-1 对树的修正称为旋转 对内 ...
- 新一代调试王者Console
随着JS在Web前端中能做的事情越来越多,责任越来越大,而地位也越来越重要.传统的alert调试方式已经渐渐不能满足前端开发的种种场景.而且alert调试方式弹出的调试信息,那个窗口着实不太美观,而且 ...
- HDU 5773 The All-purpose Zero(O(nlgn)求LIS)
http://acm.hdu.edu.cn/showproblem.php?pid=5773 题意: 求LIS,其中的0可以看做任何数. 思路: 因为0可以看做任何数,所以我们可以先不管0,先求一遍L ...
- RabbitMQ 之消息确认机制(事务+Confirm)
概述 在 Rabbitmq 中我们可以通过持久化来解决因为服务器异常而导致丢失的问题,除此之外我们还会遇到一个问题:生产者将消息发送出去之后,消息到底有没有正确到达 Rabbit 服务器呢?如果不错得 ...
- python+opencv+pyqt5控制摄像头在Qlabel上显示
import cv2 import numpy as numpy from PIL import * import sys from PyQt5.QtWidgets import * from PyQ ...
- thinkphp getField("xxxxx", true); 得到一个字段所有值组成的的数组
很多时候我们只需要一张表里某个字段的值,组成的数组 $Channel = D('channel');$channelList = $Channel->order('user_name')-> ...