PHP7:具有标量类型声明的方法拒绝键入杂耍 NULL 值,即使在 weak/coercive 模式下也是如此
PHP7: Methods with a scalar type declaration refuse to type juggle NULL values, even in weak/coercive mode
截至 PHP 7.0, the scalar type hints int
, float
, string
, and bool
can be included in method signatures. By default, these type declarations operate in weak/coercive mode (or "type juggling" mode). According to the PHP manual:
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 string.
但是即使可以将 NULL 强制转换为整数 0,具有 int
类型提示的方法将 拒绝 强制转换为 [=18 的入站值=] 到整数 0.
<?php
class MyClass
{
public function test(int $arg)
{
echo $arg;
}
}
$obj = new MyClass();
$obj->test('123'); // 123
$obj->test(false); // 0
$obj->test(null); // TypeError: Argument 1 passed to MyClass::test()
// must be of the type integer, null given
同样,即使 it is possible 将 NULL 强制转换为布尔值 false
,具有 bool
类型提示的方法将 拒绝 将 NULL
的入站值强制转换为布尔值 false
。 float
和 string
类型提示也是如此。
此行为似乎与 php.net 上的文档相矛盾。这是怎么回事?
目前没有办法允许带有标量类型提示的方法自动输入杂耍 声明类型的入站 NULL 值.
根据 RFC 负责将此功能引入 PHP 7:
The weak type checking rules for the new scalar type declarations are mostly (emphasis added) the same as those of extension and built-in PHP functions. The only exception to this is the handling of NULL: in order to be consistent with our existing type declarations for classes, callables and arrays, NULL is not accepted by default, unless it is a parameter and is explicitly given a default value of NULL.
但是,在以下情况下,NULL 值可以被接受为 NULL:
<?php
class MyClass
{
// PHP 7.0+
public function testA(int $arg = null)
{
if (null === $arg) {
echo 'The argument is NULL!';
}
}
// PHP 7.1+
// https://wiki.php.net/rfc/nullable_types
public function testB(?int $arg)
{
if (null === $arg) {
echo 'The argument is NULL!';
}
}
}
$obj = new MyClass();
$obj->testA(null); // The argument is NULL!
$obj->testB(null); // The argument is NULL!
截至 PHP 7.0, the scalar type hints int
, float
, string
, and bool
can be included in method signatures. By default, these type declarations operate in weak/coercive mode (or "type juggling" mode). According to the PHP manual:
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 string.
但是即使可以将 NULL 强制转换为整数 0,具有 int
类型提示的方法将 拒绝 强制转换为 [=18 的入站值=] 到整数 0.
<?php
class MyClass
{
public function test(int $arg)
{
echo $arg;
}
}
$obj = new MyClass();
$obj->test('123'); // 123
$obj->test(false); // 0
$obj->test(null); // TypeError: Argument 1 passed to MyClass::test()
// must be of the type integer, null given
同样,即使 it is possible 将 NULL 强制转换为布尔值 false
,具有 bool
类型提示的方法将 拒绝 将 NULL
的入站值强制转换为布尔值 false
。 float
和 string
类型提示也是如此。
此行为似乎与 php.net 上的文档相矛盾。这是怎么回事?
目前没有办法允许带有标量类型提示的方法自动输入杂耍 声明类型的入站 NULL 值.
根据 RFC 负责将此功能引入 PHP 7:
The weak type checking rules for the new scalar type declarations are mostly (emphasis added) the same as those of extension and built-in PHP functions. The only exception to this is the handling of NULL: in order to be consistent with our existing type declarations for classes, callables and arrays, NULL is not accepted by default, unless it is a parameter and is explicitly given a default value of NULL.
但是,在以下情况下,NULL 值可以被接受为 NULL:
<?php
class MyClass
{
// PHP 7.0+
public function testA(int $arg = null)
{
if (null === $arg) {
echo 'The argument is NULL!';
}
}
// PHP 7.1+
// https://wiki.php.net/rfc/nullable_types
public function testB(?int $arg)
{
if (null === $arg) {
echo 'The argument is NULL!';
}
}
}
$obj = new MyClass();
$obj->testA(null); // The argument is NULL!
$obj->testB(null); // The argument is NULL!