php 使用类型提示时不能传递空参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/14584145/
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
Cannot pass null argument when using type hinting
提问by Abdullah
The following code:
以下代码:
<?php
    class Type {
    }
    function foo(Type $t) {
    }
    foo(null);
?>
failed at run time:
运行时失败:
PHP Fatal error:  Argument 1 passed to foo() must not be null
Why is it not allowed to pass null just like other languages?
为什么不允许像其他语言一样传递 null?
回答by DonCallisto
PHP 7.1 or newer(released 2nd December 2016)
PHP 7.1 或更高版本(2016 年 12 月 2 日发布)
You can explicitly declare a variable to be nullwith this syntax
您可以null使用此语法显式声明变量
function foo(?Type $t) {
}
this will result in
这将导致
$this->foo(new Type()); // ok
$this->foo(null); // ok
$this->foo(); // error
So, if you want an optional argument you can follow the convention Type $t = nullwhereas if you need to make an argument accept both nulland its type, you can follow above example.
所以,如果你想要一个可选参数,你可以遵循约定,Type $t = null而如果你需要让一个参数同时接受null它的类型,你可以按照上面的例子。
You can read more here.
您可以在此处阅读更多内容。
PHP 7.0 or older
PHP 7.0 或更高版本
You have to add a default value like
您必须添加一个默认值,例如
function foo(Type $t = null) {
}
That way, you can pass it a null value.
这样,您可以向它传递一个空值。
This is documented in the section in the manual about Type Declarations:
这在手册中关于类型声明的部分有记录:
The declaration can be made to accept
NULLvalues if the default value of the parameter is set toNULL.
NULL如果参数的默认值设置为 ,则声明可以接受值NULL。
回答by TheOperator
Starting from PHP 7.1, nullable typesare available, as both function return types and parameters. The type ?Tcan have values of the specified Type T, or null.
从 PHP 7.1 开始,可空类型可用,作为函数返回类型和参数。该类型?T可以具有指定 TypeT或 的值null。
So, your function could look like this:
因此,您的函数可能如下所示:
function foo(?Type $t)
{
}
As soon as you can work with PHP 7.1, this notation should be preferred over function foo(Type $t = null), because it still forces the caller to explicitly specify an argument for the parameter $t.
一旦您可以使用 PHP 7.1,就应该优先使用这种表示法function foo(Type $t = null),因为它仍然强制调用者为参数 明确指定一个参数$t。
回答by SeanWM
回答by Fabian Schmengler
As other answers already mentioned, this is only possible if you specify nullas the default value.
正如已经提到的其他答案,只有在您指定null为默认值时才有可能。
But the cleanest type-safe object oriented solution would be a NullObject:
但最干净的类型安全面向对象解决方案是NullObject:
interface FooInterface
{
    function bar();
}
class Foo implements FooInterface
{
    public function bar()
    {
        return 'i am an object';
    }
}
class NullFoo implements FooInterface
{
    public function bar()
    {
        return 'i am null (but you still can use my interface)';
    }
}
Usage:
用法:
function bar_my_foo(FooInterface $foo)
{
    if ($foo instanceof NullFoo) {
        // special handling of null values may go here
    }
    echo $foo->bar();
}
bar_my_foo(new NullFoo);

