private static ?Singleton $instance = null;
1
jfcherng 2020-12-03 14:45:40 +08:00 1
php 7.1, 關鍵字 nullable
|
2
Still4 2020-12-03 15:05:59 +08:00 1
我这一般是这么用的
public function initTask($task_info, $task_num, $task_total): ?Thread public function initTask($task_info, $task_num, $task_total): Thread 当返回结果可能为空的时候用前一个,不这么写返回空会报错 |
3
xoxo419 OP @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 |
4
jfcherng 2020-12-03 15:43:36 +08:00
如果你問的不是那個 "?" 的話, typed property php 7.4
https://www.php.net/manual/en/migration74.new-features.php#migration74.new-features.core.typed-properties |