PHP 8.3已经如期发布,看一下新版特带来哪些特性!
常量类型化(Typed class constants) 定义常量目前可以标识类型了!
8.3 之前
1 2 3 4 5 6 7 8 9 interface I { const PHP = 'PHP 8.2' ; } class Foo implements I { const PHP = []; }
8.3 之后
1 2 3 4 5 6 7 8 9 interface I { const string PHP = 'PHP 8.3' ; } class Foo implements I { const string PHP = []; }
动态类常量获取(Dynamic class constant fetch) 8.3 之前
1 2 3 4 5 6 7 class Foo { const PHP = 'PHP 8.2' ; } $searchableConstant = 'PHP' ;var_dump (constant (Foo ::class . "::{$searchableConstant} " ));
8.3 之后
1 2 3 4 5 6 7 class Foo { const PHP = 'PHP 8.3' ; } $searchableConstant = 'PHP' ;var_dump (Foo ::{$searchableConstant });
#[\Override] 新注解 通过将#[\Override]属性添加到方法中,PHP将确保在父类或实现的接口中存在具有相同名称的方法。如下面MyTest
的父类TestCase
中没有taerDown
方法将抛出异常
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 use PHPUnit \Framework \TestCase ;final class MyTest extends TestCase { protected $logFile ; protected function setUp ( ): void { $this ->logFile = fopen ('/tmp/logfile' , 'w' ); } #[\Override ] protected function taerDown ( ): void { fclose ($this ->logFile); unlink ('/tmp/logfile' ); } }
深度克隆只读属性(Deep-cloning of readonly properties) 只读属性现在可以在__clone方法中修改一次,以启用只读属性的深度克隆。
8.3 之前
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 class PHP { public string $version = '8.2' ; } readonly class Foo { public function __construct ( public PHP $php ) {} public function __clone ( ): void { $this ->php = clone $this ->php; } } $instance = new Foo (new PHP ());$cloned = clone $instance ;
8.3 之后
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 class PHP { public string $version = '8.2' ; } readonly class Foo { public function __construct ( public PHP $php ) {} public function __clone ( ): void { $this ->php = clone $this ->php; } } $instance = new Foo (new PHP ());$cloned = clone $instance ;$cloned ->php->version = '8.3' ;
json_validate 函数 8.3 之前验证json 需要json_decode之后根据json_last_error()错误去判断,如今8.3新增json_validate函数支持
Randomizer 新特性
getBytesFromString()
getFloat()
nextFloat()
其他 其余bug和特性请移步到,php官方8.3发布页面介绍 。