PHP7(?string 或?int)中类型声明前问号的用途是什么?
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
你能告诉我这个怎么称呼吗? ?string
和 string
用法示例:
public function (?string $parameter1, string $parameter2) {}
我想了解一些关于它们的信息,但我在 PHP 文档和 google 中都找不到它们。它们有什么区别?
表示参数允许作为指定类型或NULL传递:
什么是可空类型?
在PHP7.1中引入,
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.
在参数中
function test(?string $parameter1, string $parameter2) {
var_dump($parameter1, $parameter2);
}
test("foo", "bar");
test(null, "foo");
test("foo", null); // Uncaught TypeError: Argument 2 passed to test() must be of the type string, null given,
使用可变参数
在这个例子中,你可以传递null
或string
参数:
function acceptOnlyStrings(string ...$parameters) { }
function acceptStringsAndNull(?string ...$parameters) { }
acceptOnlyStrings('foo', null, 'baz'); // Uncaught TypeError: Argument #2 must be of type string, null given
acceptStringsAndNull('foo', null, 'baz'); // OK
Return类型
函数的return类型也可以是可空类型,允许returnnull
或指定类型
function error_func(): int {
return null ; // Uncaught TypeError: Return value must be of the type integer
}
function valid_func(): ?int {
return null ; // OK
}
function valid_int_func(): ?int {
return 2 ; // OK
}
属性 类型(截至 PHP 7.4)
属性 的类型可以是可为 null 的类型。
class Foo
{
private object $foo = null; // ERROR : cannot be null
private ?object $bar = null; // OK : can be null (nullable type)
private object $baz; // OK : uninitialized value
}
另见:
可为空的联合类型(从 PHP 8.0 开始)
从 PHP 8 开始,"?T
表示法被认为是 shorthand 对于 T|null
" 的常见情况
class Foo
{
private ?object $bar = null; // as of PHP 7.1+
private object|null $baz = null; // as of PHP 8.0
}
错误
如果运行 PHP版本低于PHP 7.1,会抛出语法错误:
syntax error, unexpected '?', expecting variable (T_VARIABLE)
应删除 ?
运算符。
PHP 7.1+
function foo(?int $value) { }
PHP 7.0 或更低
/**
* @var int|null
*/
function foo($value) { }
参考资料
自 PHP 7.4 起:Class 属性类型声明。
自 PHP 8.0 起:可空联合类型
函数参数中 string
之前的问号表示 nullable type。在上面的示例中,允许 $parameter1
must 具有 NULL
值,而 $parameter2
则不允许;它必须包含一个有效的字符串。
Parameters with a nullable type do not have a default value. If omitted the value does not default to null and will result in an error:
function f(?callable $p) { }
f(); // invalid; function f does not have a default
这大致相当于
public function (string $parameter1 = null, string $parameter2) {}
除了参数还是必须的,省略参数会报错
特别是在这种情况下,第二个参数是必需的,使用 =null
会使第一个参数成为可选参数,这实际上不起作用。当然可以,但我的意思是它实际上并没有使它成为可选的,这是默认值的主要目的。
所以使用
public function (?string $parameter1, string $parameter2) {}
在这种情况下,语法上更有意义。
你能告诉我这个怎么称呼吗? ?string
和 string
用法示例:
public function (?string $parameter1, string $parameter2) {}
我想了解一些关于它们的信息,但我在 PHP 文档和 google 中都找不到它们。它们有什么区别?
表示参数允许作为指定类型或NULL传递:
什么是可空类型?
在PHP7.1中引入,
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.
在参数中
function test(?string $parameter1, string $parameter2) {
var_dump($parameter1, $parameter2);
}
test("foo", "bar");
test(null, "foo");
test("foo", null); // Uncaught TypeError: Argument 2 passed to test() must be of the type string, null given,
使用可变参数
在这个例子中,你可以传递null
或string
参数:
function acceptOnlyStrings(string ...$parameters) { }
function acceptStringsAndNull(?string ...$parameters) { }
acceptOnlyStrings('foo', null, 'baz'); // Uncaught TypeError: Argument #2 must be of type string, null given
acceptStringsAndNull('foo', null, 'baz'); // OK
Return类型
函数的return类型也可以是可空类型,允许returnnull
或指定类型
function error_func(): int {
return null ; // Uncaught TypeError: Return value must be of the type integer
}
function valid_func(): ?int {
return null ; // OK
}
function valid_int_func(): ?int {
return 2 ; // OK
}
属性 类型(截至 PHP 7.4)
属性 的类型可以是可为 null 的类型。
class Foo
{
private object $foo = null; // ERROR : cannot be null
private ?object $bar = null; // OK : can be null (nullable type)
private object $baz; // OK : uninitialized value
}
另见:
可为空的联合类型(从 PHP 8.0 开始)
从 PHP 8 开始,"?T
表示法被认为是 shorthand 对于 T|null
" 的常见情况
class Foo
{
private ?object $bar = null; // as of PHP 7.1+
private object|null $baz = null; // as of PHP 8.0
}
错误
如果运行 PHP版本低于PHP 7.1,会抛出语法错误:
syntax error, unexpected '?', expecting variable (T_VARIABLE)
应删除 ?
运算符。
PHP 7.1+
function foo(?int $value) { }
PHP 7.0 或更低
/**
* @var int|null
*/
function foo($value) { }
参考资料
自 PHP 7.4 起:Class 属性类型声明。
自 PHP 8.0 起:可空联合类型
函数参数中 string
之前的问号表示 nullable type。在上面的示例中,允许 $parameter1
must 具有 NULL
值,而 $parameter2
则不允许;它必须包含一个有效的字符串。
Parameters with a nullable type do not have a default value. If omitted the value does not default to null and will result in an error:
function f(?callable $p) { }
f(); // invalid; function f does not have a default
这大致相当于
public function (string $parameter1 = null, string $parameter2) {}
除了参数还是必须的,省略参数会报错
特别是在这种情况下,第二个参数是必需的,使用 =null
会使第一个参数成为可选参数,这实际上不起作用。当然可以,但我的意思是它实际上并没有使它成为可选的,这是默认值的主要目的。
所以使用
public function (?string $parameter1, string $parameter2) {}
在这种情况下,语法上更有意义。