【问题标题】:What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?PHP7中类型声明之前的问号(?string或?int)的目的是什么?
【发布时间】:2018-07-05 03:59:37
【问题描述】:

你能告诉我这是怎么称呼的吗? ?stringstring

使用示例:

public function (?string $parameter1, string $parameter2) {}

我想了解一些关于它们的信息,但我在 PHP 文档和谷歌中都找不到它们。它们有什么区别?

【问题讨论】:

    标签: php php-7


    【解决方案1】:

    它被称为Nullable type,在 PHP 7.1 中引入。

    如果存在 Nullable 类型(带有 ?)参数或相同类型的值,则可以传递 NULL 值。

    参数:

    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或指定类型。

    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 起):

    属性的类型可以是可空类型。

    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 表示法被视为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,会抛出语法错误:

    语法错误,意外的“?”,预期变量(T_VARIABLE)

    ? 运算符应该被删除。

    PHP 7.1+

    function foo(?int $value) { }
    

    PHP 7.0 或更低

    /** 
     * @var int|null 
     */
    function foo($value) { }
    

    参考

    截至PHP 7.1

    现在可以通过在类型名称前加上问号来将参数和返回值的类型声明标记为可为空。这表示除了指定的类型外,NULL 也可以分别作为参数传递或作为值返回。

    截至PHP 7.4:类属性类型声明。

    截至PHP 8.0:可空联合类型

    【讨论】:

      【解决方案2】:

      函数参数中string 之前的问号表示nullable type。在上面的示例中,$parameter1 must 允许具有 NULL 值,而 $parameter2 不允许;它必须包含一个有效的字符串。

      具有可空类型的参数没有默认值。如果省略,则该值不会默认为 null 并会导致错误:

      函数 f(?callable $p) { }
      F(); // 无效的;函数 f 没有默认值

      【讨论】:

        【解决方案3】:

        表示参数允许作为指定类型或NULL传递:

        http://php.net/manual/en/migration71.new-features.php

        【讨论】:

          【解决方案4】:

          这大致相当于

           public function (string $parameter1 = null, string $parameter2) {}
          

          除非参数仍然是必需的,如果省略参数会报错。

          特别是在这种情况下,第二个参数是必需的,使用=null 将使第一个参数成为可选的,这实际上不起作用。当然可以,但我的意思是它实际上并没有使它成为可选的,这是默认值的主要目的。

          所以使用

          public function (?string $parameter1, string $parameter2) {}
          

          在这种情况下,语法上更有意义。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2022-01-02
            • 2017-01-09
            • 1970-01-01
            • 2017-11-02
            • 1970-01-01
            • 2012-11-17
            相关资源
            最近更新 更多