PHP 8.3已经如期发布,看一下新版特带来哪些特性!
常量类型化(Typed class constants)
定义常量目前可以标识类型了!
8.3 之前
interface I {
// 过往历史长河中总是认为PHP常量总是一个字符串
const PHP = 'PHP 8.2';
}
class Foo implements I {
// 但是实现类可能会将其定义为数组。
const PHP = [];
}
8.3 之后
interface I {
const string PHP = 'PHP 8.3';
}
class Foo implements I {
const string PHP = [];
}
//代码会发生致命错误:无法将数组用作类常量的值