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:
use
keyword is required. $this
can be used like a normal closure. return
statement. You can read them in depth here.
Class attribute can prompt the type:
class A { public string $name; public Foo $foo; }
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) { /* … */ } }
No more need to do this:
$data['date'] = $data['date'] ?? new DateTime();
You can do this:
$data['date'] ??= new DateTime();
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.
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 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)
RFC adds two new magic methods: __serialize
and __unserialize
. The differences between these methods and __sleep
and __wakeup
are discussed in RFC.
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.
This is not technically an update related to PHP 7.4, but it is worth mentioning: the voting rules for RFCs have changed.
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 不再支持维护,核心团队决定在 PHP 7.4 中删除它的默认安装。
对如何使用散列库进行内部更改,以便用户可以更轻松地使用它们。
ext/wwdx
RFC
此数据交换格式从未标准化,现在已经弃用该扩展。
短开标签 <?
已被弃用,将在 PHP 8 中删除。短声明标记 <?=
不受影响。
三元运算符在 PHP 中有一些奇怪的怪癖。此 RFC 为嵌套的三元语句添加了弃用。在 PHP 8 中,此弃用将转换为编译时错误。
1 ? 2 : 3 ? 4 : 5; // deprecated (1 ? 2 : 3) ? 4 : 5; // ok
升级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!