xoxo419
V2EX  ›  PHP

PHP 中对象属性?中表示什么意思 哪个版本开始支持的?

  •  
  •   xoxo419 · Dec 3, 2020 · 2682 views
    This topic created in 2182 days ago, the information mentioned may be changed or developed.
    4 replies  •  2020-12-03 15:43:36 +08:00
    jfcherng
    jfcherng
    Dec 3, 2020❤️1
    1
    php 7.1, 關鍵字 nullable
    Still4
    Still4
    Dec 3, 2020❤️1
    2
    我这一般是这么用的

    public function initTask($task_info, $task_num, $task_total): ?Thread

    public function initTask($task_info, $task_num, $task_total): Thread

    当返回结果可能为空的时候用前一个,不这么写返回空会报错
    xoxo419
    xoxo419
    OP
    Dec 3, 2020
    3
    @jfcherng
    @Still4

    用 PHP7.2 执行报错误

    ```
    There was 1 error:

    1) DesignPatterns\Creational\Singleton\Tests\SingletonTest::testUniqueness
    ParseError: syntax error, unexpected '?', expecting function (T_FUNCTION) or const (T_CONST)

    /var/www/base/test/php/DesignPatterns/Creational/Singleton/Singleton.php:7
    /var/www/base/test/php/DesignPatterns/Creational/Singleton/Tests/SingletonTest.php:12

    ```

    用 php7.4 执行 OK
    jfcherng
    jfcherng
    Dec 3, 2020
    4
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2055 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 89ms · UTC 15:21 · PVG 23:21 · LAX 07:21 · JFK 10:21
    ♥ Do have faith in what you're doing.