php 类型提示:默认参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8522984/
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 04:54:43  来源:igfitidea点击:

Type Hinting: Default Parameters

phptype-hintingdefault-parameters

提问by ThinkingMonkey

PHP 5 Type Hinting

PHP 5 类型提示

PHP 5 introduces Type Hinting.Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays(since PHP 5.1). However, if NULLis used as the default parameter value, it will be allowed as an argument for any later call.

PHP 5 引入了类型提示。函数现在能够强制参数为对象(通过在函数原型中指定类的名称)或arrays自 PHP 5.1 起)。但是,如果NULL用作默认参数值,它将被允许作为任何以后调用的参数。

The following excerpt from the above:

以下摘录自上述内容:

if NULLis used as the default parameter value, it will be allowed as an argument for any later call.

如果NULL用作默认参数值,它将被允许作为任何以后调用的参数。

Does the above mean:

上面的意思是:

if default parameters are to used use with type hinting, it can have only have NULLas the default value.

如果默认参数与类型提示一起使用,则它只能NULL作为默认值。

i.e.the code in code1 is wrong and results in:

code1 中的代码是错误的,结果是:

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

致命错误:带有类类型提示的参数的默认值只能是 NULL

code1:

代码1:

 function setName ( string $name = "happ") {
  ...
  }

Where as code in code2 is right:

code2 中的代码是正确的:

code2:

代码2:

 function setName ( string $name = NULL) {
  ...
  }

Why is this constraint assigned in php?

为什么在 php 中分配了这个约束?

回答by Alex Howansky

You can't typehint strings, you can only typehint objects and arrays, so this is incorrect:

你不能打字提示字符串,你只能打字提示对象和数组,所以这是不正确的:

function setName ( string $name = "happ") {

(The reason you don't get a compile-time error here is because PHP is interpreting "string" as the name of a class.)

(这里没有出现编译时错误的原因是 PHP 将“字符串”解释为类的名称。)

The wording in the docs means that if you do this:

文档中的措辞意味着如果你这样做:

function foo(Foo $arg) {

Then the argument passed to foo() must be an instance of object Foo. But if you do this:

那么传递给 foo() 的参数必须是对象 Foo 的一个实例。但如果你这样做:

function foo(Foo $arg = null) {

Then the argument passed to foo() can either be an instance of object Foo, or null. Note also that if you do this:

然后传递给 foo() 的参数可以是对象 Foo 的实例,也可以是 null。另请注意,如果您这样做:

function foo(array $foo = array(1, 2, 3))

Then you can't call foo(null). If you want this functionality, you can do something like this:

那么你不能调用 foo(null)。如果你想要这个功能,你可以做这样的事情:

function foo(array $foo = null) {
    if ($foo === null) {
        $foo = array(1, 2, 3);
    }

[Edit 1]As of PHP 5.4, you can typehint callable:

[Edit 1]从 PHP 5.4 开始,您可以输入提示callable

function foo(callable $callback) {
    call_user_func($callback);
}

[Edit 2]As of PHP 7.0, you can typehint bool, float, int, and string. This makes the code in the question valid syntax. As of PHP 7.1, you can typehint iterable.

[Edit 2]由于PHP 7.0,你可以typehint boolfloatint,和string。这使得问题中的代码语法有效。从 PHP 7.1 开始,您可以 typehint iterable

回答by kenorb

Type declarations(also known as type hints in PHP 5) of a stringtype are supported in PHP 7.

stringPHP 7 支持类型的类型声明(在 PHP 5 中也称为类型提示)。

The valid typesare:

有效的类型是:

  • Class/interface name (>=PHP 5.0.0);
  • self(>=PHP 5.0.0);
  • array(>=PHP 5.1.0);
  • callable(>=PHP 5.4.0);
  • bool, float, int, string(>=PHP 7.0.0);
  • iterable- either an array or an instanceof Traversable (>=PHP 7.1.0).
  • 类/接口名称(>=PHP 5.0.0);
  • self(>=PHP 5.0.0);
  • array(>=PHP 5.1.0);
  • callable(>=PHP 5.4.0);
  • bool, float, int, string(>=PHP 7.0.0);
  • iterable- 数组或 Traversable 实例(>=PHP 7.1.0)。

回答by Chris

This is a matter of compilation time versus run time values. At compilation only literal values (strings, numbers, booleans and NULL) are allowed. The PHP processor can't know about all the possible classes at this time and so you can't create an instance of an object in the function arguments.

这是编译时间与运行时间值的问题。编译时只允许使用文字值(字符串、数字、布尔值和 NULL)。PHP 处理器此时无法知道所有可能的类,因此您无法在函数参数中创建对象的实例。

What I'm expecting from the excerpt is that, while normally passing NULL into a type hinted function will throw an Exception/Error. If you set a default as NULL then it won't throw this exception if NULL is passed. I haven't tested it, just what I would expect.

我对摘录的期望是,虽然通常将 NULL 传递给类型提示的函数会引发异常/错误。如果您将默认值设置为 NULL,则在传递 NULL 时不会抛出此异常。我还没有测试它,正是我所期望的。