xoxo419
V2EX  ›  PHP

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

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

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

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

    当返回结果可能为空的时候用前一个,不这么写返回空会报错
    xoxo419
        3
    xoxo419  
    OP
       Dec 3, 2020
    @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
        4
    jfcherng  
       Dec 3, 2020
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   873 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 19:54 · PVG 03:54 · LAX 12:54 · JFK 15:54
    ♥ Do have faith in what you're doing.