您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

介绍php7.3到php7.4新特性

2026/2/18 7:28:44发布12次查看
1、对象属性现在支持类型申明
<?phpclass user { public int $id; public string $name;}?>
上列例子中将会强制转化$id为integer类型,$name为字符串类型
推荐(免费):php7
2、箭头函数
现在箭头函数会提供一个短语法用来定义那些在作用域内值申明不明确的函数
<?php$factor = 10;$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);// $nums = array(10, 20, 30, 40);?>
3、有限返回类型协方差和参数类型协方差(谷歌翻译的)
下列代码将会运行
<?phpclass a {}class b extends a {}class producer { public function method(): a {}}class childproducer extends producer { public function method(): b {}}?>
仅当使用自动加载时,才提供完全差异支持。 在单个文件内,只能使用非循环类型引用,因为所有类在被引用之前都必须可用
4、空合并分配运算符
<?php$array['key'] = computedefault();// is roughly equivalent toif (!isset($array['key'])) { $array['key'] = computedefault();}?>
5、unpacking inside arrays(谷歌翻译是 在数组内部解包,看这不对,先用英文吧)
<?php$parts = ['apple', 'pear'];$fruits = ['banana', 'orange', ...$parts, 'watermelon'];// ['banana', 'orange', 'apple', 'pear', 'watermelon'];?>
6、数值字符分隔符
数字字符可以在数字之间包含下划线。
<?php6.674_083e-11; // float299_792_458; // decimal0xcafe_f00d; // hexadecimal0b0101_1111; // binary?>
7、弱参考(weak references)
弱参考允许编程人员保留对不能防止被销毁的对象的参考(weak references allow the programmer to retain a reference to an object that does not prevent the object from being destroyed.)
8、允许__tostring()的异常
现在可以通过__tostring()抛出异常,在之前的版本这会导致一个致命错误,字符串转换中现有的可导致致命错误已转换为抛出异常。
剩下的扩展以后再看
以上就是介绍php7.3到php7.4新特性的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product