New features in PHP 7.4
- Release date may be around December 2019, yet to be confirmed
- Short closures, Allows for more concise single-line writing
- Preloading to improve performance
- Type attributes in classes
- Custom object serialization Adds a (de)serialized object New approach
- Improved type differences
- Simplified Null coalescing operator
- FFI opens up new opportunities for PHP extension development
- Deprecated short open tags
- Support for spread operators in arrays
- Read the following to learn more
Short closureRFC
Short closure Package enables more concise single-line writing.
array_map(function (User $user) { return $user->id; }, $users)
array_map(fn(User $user) => $user->id, $users)
Some notes about short closures:
- has access to the parent scope, no
use
keyword is required. -
$this
can be used like a normal closure. - Short closures can only contain one line, which is the
return
statement.
You can read them in depth here.
Type attribute RFC
Class attribute can prompt the type:
class A { public string $name; public Foo $foo; }
Improved type differenceRFC
I've written about PHP's type system before, so it's nice to see some of PHP's core being improved.
Type differences are a topic worthy of a blog post; in short: you will be able to use covariate return types. . .
class ParentType {} class ChildType extends ParentType {} class A { public function covariantReturnTypes(): ParentType { /* … */ } } class B extends A { public function covariantReturnTypes(): ChildType { /* … */ } }
. . . and inverse variables.
class A { public function contraVariantArguments(ChildType $type) { /* … */ } } class B extends A { public function contraVariantArguments(ParentType $type) { /* … */ } }
Null Coalescing OperatorRFC
No more need to do this:
$data['date'] = $data['date'] ?? new DateTime();
You can do this:
$data['date'] ??= new DateTime();
Array Spread operatorRFC
You can now use the spread operator with arrays:
$arrayA = [1, 2, 3]; $arrayB = [4, 5]; $result = [0, ...$arrayA, ...$arrayB, 6 ,7]; // [0, 1, 2, 3, 4, 5, 6, 7]
Please note that this only works for arrays with numeric keys.
External Function Interface RFC
The External Function Interface, or FFI for short, allows C code to be called from the user area. This means that PHP extensions can be written in pure PHP.
It should be noted that this is a complex topic. You still need C knowledge to use this feature correctly.
Preloading RFC
Preloading is an exciting new feature in the core of PHP that can bring unpredictable performance improvements.
In short: If you are using a framework today, its files must be loaded and recompiled on every request. Preloading allows the server to load PHP files in memory on startup and make them persistent for all subsequent requests (as long as there is no power outage).
Performance improvements of course come at a price: if the source file of the preloaded file changes, the server must be restarted (if you have any objections to this part, please see the RFC for details)
Custom objects SerializationRFC
RFC adds two new magic methods: __serialize
and __unserialize
. The differences between these methods and __sleep
and __wakeup
are discussed in RFC.
Connection PriorityRFC
If you wrote something like:
echo "sum: " . $a + $b;
PHP used to compile it like this:
echo ("sum: " . $a) + $b;
And PHP 8 will make it compile as follows:
echo "sum :" . ($a + $b);
When encountering a ' ' or a '.' before an expression without parentheses, PHP 7.4 will prompt for deprecation. warn.
RFC voting process improvements
This is not technically an update related to PHP 7.4, but it is worth mentioning: the voting rules for RFCs have changed.
- They will always need 2/3 majority support to pass.
- The voting time is not short, all RFCs must be open for at least 2 weeks.
Reflection on References RFC
Libraries like Symfony's var dumper rely heavily on the reflection API to dump variables reliably. Previously, there was no proper reflection support for references, resulting in these libraries relying on hackers to detect reflections.
PHP 7. 4 added a ReflectionReference
class to solve this problem.
添加 mb_str_split
函数 RFC
此函数提供与 str_split
多字节字符串相同的功能。
永久支持 ext-hash
RFC
正如标题所说,此扩展现在可在所有 PHP 安装中永久支持使用。
默认不启用 PEAR EXTERNALS
由于 PEAR 不再支持维护,核心团队决定在 PHP 7.4 中删除它的默认安装。
密码哈希注册表RFC
对如何使用散列库进行内部更改,以便用户可以更轻松地使用它们。
弃用ext/wwdx
RFC
此数据交换格式从未标准化,现在已经弃用该扩展。
PHP 短标签被弃用 RFC
短开标签 已被弃用,将在 PHP 8 中删除。短声明标记
=
不受影响。
左关联三元运算符被弃用 RFC
三元运算符在 PHP 中有一些奇怪的怪癖。此 RFC 为嵌套的三元语句添加了弃用。在 PHP 8 中,此弃用将转换为编译时错误。
1 ? 2 : 3 ? 4 : 5; // deprecated (1 ? 2 : 3) ? 4 : 5; // ok
向后不兼容的更改 UPGRADING
升级PHP版本时,您应该始终查看完整的 UPGRADING 文档。
以下是一些突出显示的向后不兼容的更改:
- 调用
var_dump
一个DateTime
或DateTimeImmutable
实例后面将不再保留对象的可访问属性。 -
openssl_random_pseudo_bytes
将在错误情况下抛出异常。 - 尝试序列化一个
PDO
或PDOStatement
实例将生成一个Exception
而不是一个PDOException
。 调用
get_object_vars()
上的ArrayObject
实例将返回的属性ArrayObject
本身,而不是包装的数组或对象的值。请注意,(array)
强制转换不受影响。
更多PHP相关技术文章,请访问PHP教程栏目进行学习!
The above is the detailed content of Learn about the new features of PHP 7.4 in three minutes. For more information, please follow other related articles on the PHP Chinese website!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在PHP中,可以利用implode()函数的第一个参数来设置没有分隔符,该函数的第一个参数用于规定数组元素之间放置的内容,默认是空字符串,也可将第一个参数设置为空,语法为“implode(数组)”或者“implode("",数组)”。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Mac version
God-level code editing software (SublimeText3)

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Notepad++7.3.1
Easy-to-use and free code editor

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.
