PHP:bool 与 boolean 类型提示

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/44009037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:57:07  来源:igfitidea点击:

PHP: bool vs boolean type hinting

phpbooleandefault-valuetype-hinting

提问by Jimmy P

I've been trying to use type hinting more in PHP. Today I was writing a function that takes a boolean with a default parameter and I noticed that a function of the form

我一直在尝试在 PHP 中更多地使用类型提示。今天我正在编写一个函数,它接受一个带有默认参数的布尔值,我注意到一个形式为

function foo(boolean $bar = false) {
    var_dump($bar);
}

actually throws a fatal error:

实际上抛出一个致命错误:

Default value for parameters with a class type hint can only be NULL

具有类类型提示的参数的默认值只能为 NULL

While a function of the similar form

而类似形式的函数

function foo(bool $bar = false) {
    var_dump($bar);
}

does not. However, both

才不是。然而,两者

var_dump((bool) $bar);
var_dump((boolean) $bar);

give the exact same output

给出完全相同的输出

:boolean false

:布尔假

Why is this? Is this similar to the wrapper classes in Java?

为什么是这样?这与 Java 中的包装类相似吗?

回答by Niet the Dark Absol

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

Warning
Aliases for the above scalar types are not supported. 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 bool:

<?php
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


不支持上述标量类型的警告别名。相反,它们被视为类或接口名称。例如,使用 boolean 作为参数或返回类型将需要一个参数或返回值,它是类或接口 boolean 的实例,而不是 bool 类型:

<?php
function?test(boolean?$param)?{}
test(true);
?>

上面的例子将输出:

致命错误:未捕获的类型错误:传递给 test() 的参数 1 必须是布尔值的实例,给定的布尔值

So in a nutshell, booleanis an alias for bool, and aliases don't work in type hints.
Use the "real" name: bool

所以简而言之,boolean是 的别名bool,并且别名在类型提示中不起作用。
使用“真实”名称:bool



There are no similarity between Type Hintingand Type Casting.

Type Hinting和之间没有相似之处Type Casting

Type hintingis something like that you are telling your function which type should be accepted.

类型提示类似于您告诉函数应该接受哪种类型。

Type castingis to "switching" between types.

类型转换是在类型之间“切换”。

The casts allowed are:

(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL (PHP 5)

允许的演员表是:

(int), (integer) - cast to integer
(bool), (boolean) - cast to boolean
(float), (double), (real) - cast to float
(string) - cast to string
(array) - cast to array
(object) - cast to object
(unset) - cast to NULL (PHP 5)

In php type castingboth (bool) and (boolean) are the same.

在 php类型转换中(bool) 和 (boolean) 是相同的。