博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php+跑buth,php 中函数获取可变参数的方法, 这个语法有点像 golang 语言中的
阅读量:1532 次
发布时间:2019-04-21

本文共 5068 字,大约阅读时间需要 16 分钟。

Only valid typehint for boolean is bool. As per documentation boolean isn't recognized as alias of bool in typehints. Instead it is treated as class name. Same goes for int(scalar) and integer(class name), which will result in error

TypeError: Argument 1 passed to a() must be an instance of integer, integer given

In this specific case object of class boolean is expected but true(bool, scalar) is passed.

Valid code is

functiona(bool$value){var_dump($value);}a(true);

which result is

bool(true)

--------------------------------------------------------------------------------

1. php函数调用中获取变长参数的方法, ...token ,类似于golang语言中的。

2. php 中也有类似与javascript 中的 "use strict ";严格模式。

Strict typing¶

By default, PHP will coerce values of the wrong type into the expected scalar type if possible. For example, a function that is given an integer for a parameter that expects a string will get a variable of type

It is possible to enable strict mode on a per-file basis. In strict mode, only a variable of exact type of the type declaration will be accepted, or a TypeError will be thrown. The only exception to this rule is that an integer may be given to a function expecting a float. Function calls from within internal functions will not be affected by the strict_types declaration.

To enable strict mode, the declare statement is used with the strict_types declaration:

declare(strict_types=1);

function sum(int $a, int $b) {

return $a + $b;

}

var_dump(sum(1, 2));

var_dump(sum(1.5, 2.5));

?>

3.Type declarations¶

Note:

Type declarations were also known as type hints in PHP 5.

Type declarations allow functions to require that parameters are of a certain type at call time. If the given value is of the incorrect type, then an error is generated: in PHP 5, this will be a recoverable fatal error, while PHP 7 will throw a TypeError exception.

To specify a type declaration, the type name should be added before the parameter name. The declaration can be made to accept NULL values if the default value of the parameter is set to NULL.

Valid types¶

Type

Description

Minimum PHP version

Class/interface name

The parameter must be an instanceof the given class or interface name.

PHP 5.0.0

self

The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods.

PHP 5.0.0

The parameter must be an

PHP 5.1.0

The parameter must be a valid

PHP 5.4.0

The parameter must be a

PHP 7.0.0

The parameter must be a floating point number.

PHP 7.0.0

The parameter must be an

PHP 7.0.0

The parameter must be a

PHP 7.0.0

iterable

The parameter must be either an

PHP 7.1.0

Warning

Aliases for the above scalar types are not supported(意思是: 只能用int, 而不能用integer, 只能用bool,不能用boolean.). Instead, they are treated as class or interface names. For example, using boolean as a parameter or return type will require an argument or return value that is an instanceof the class or interface boolean, rather than of type

function test(boolean $param) {}

test(true);

?>

The above example will output:

Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of boolean, boolean given, called in - on line 1 and defined in -:1

---------------------------------------------------------------------------------------------------------------

... in PHP 5.6+¶

In PHP 5.6 and later, argument lists may include the ... token to denote that the function accepts a variable number of arguments. The arguments will be passed into the given variable as an array; for example:

Example #13 Using ... to access variable arguments

function sum(...$numbers) {

$acc = 0;

foreach ($numbers as $n) {

$acc += $n;

}

return $acc;

}

echo sum(1, 2, 3, 4);

?>

The above example will output:

10

You can also use ... when calling functions to unpack an array or Traversable variable or literal into the argument list:

Example #14 Using ... to provide arguments

function add($a, $b) {

return $a + $b;

}

echo add(...[1, 2])."\n";

$a = [1, 2];

echo add(...$a);

?>

The above example will output:

3

3

You may specify normal positional arguments before the ... token. In this case, only the trailing arguments that don't match a positional argument will be added to the array generated by ....

It is also possible to add a type hint before the ... token. If this is present, then all arguments captured by ... must be objects of the hinted class.

Example #15 Type hinted variable arguments

function total_intervals($unit, DateInterval ...$intervals) {

$time = 0;

foreach ($intervals as $interval) {

$time += $interval->$unit;

}

return $time;

}

$a = new DateInterval('P1D');

$b = new DateInterval('P2D');

echo total_intervals('d', $a, $b).' days';

// This will fail, since null isn't a DateInterval object.

echo total_intervals('d', null);

?>

The above example will output:

3 days

Catchable fatal error: Argument 2 passed to total_intervals() must be an instance of DateInterval, null given, called in - on line 14 and defined in - on line 2

Finally, you may also pass variable arguments by reference by prefixing the ... with an ampersand (&).

转载地址:http://vrkdy.baihongyu.com/

你可能感兴趣的文章
电子线路设计技巧6:Boost电路的参数设计
查看>>
电子线路设计技巧7:UC3843A升压电路中振荡频率和占空比的确定方法
查看>>
电子线路设计技巧8:UC3843A升压电路中电压反馈环节的设计方法
查看>>
AUTOCAD学习笔记9:多级放大电路的绘制
查看>>
AUTOCAD学习笔记10:互补功率放大电路的绘制
查看>>
电路基础学习笔记4:复杂直流电路分析2
查看>>
python学习笔记7:对象引用与对象
查看>>
电路基础学习笔记5:实验验证电压源与电流源的等效变换
查看>>
电路基础学习笔记6:实验验证戴维南定理
查看>>
Verilog学习笔记7:Quartus Prime的安装
查看>>
Verilog学习笔记8:Quartus Prime的更新
查看>>
Verilog学习笔记9:USB Blaster下载器的安装
查看>>
Verilog学习笔记10:建立Quartus Prime工程
查看>>
Verilog学习笔记11:一个完整的工作流程
查看>>
计算机网络基础12:IPV4与IPV6的区别
查看>>
计算机网络基础13:使用RouterOS调试IPV6
查看>>
STM32开发笔记67: 在keil中使用ST-Link不能成功下载的解决方法
查看>>
常用元器件使用方法23:Lora模块E32_400T20S的使用方法
查看>>
Python语言系统学习1:关于“人生苦短,我学Python”这句话的由来
查看>>
STM32开发笔记68: keil中使用ST-Link不能成功下载的真实原因
查看>>