Home  >  Article  >  Backend Development  >  Learn about the new features of PHP 7.4 in three minutes

Learn about the new features of PHP 7.4 in three minutes

步履不停
步履不停Original
2019-06-25 14:29:206168browse

Learn about the new features of PHP 7.4 in three minutes

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 一个 DateTimeDateTimeImmutable 实例后面将不再保留对象的可访问属性。
  • openssl_random_pseudo_bytes 将在错误情况下抛出异常。
  • 尝试序列化一个 PDOPDOStatement 实例将生成一个 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn