34

PHP 7.4 in 7 code blocks

 3 years ago
source link: https://stitcher.io/blog/php-74-in-7-code-blocks
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP 7.4, the last edition in the 7.* series, brings lots of new and handy changes. This post lists the highlights, though there's much more to this release. You can read all about the full release in this post about what's new in PHP 7.4 .

array_map(
    fn(User $user) => $user->id,
    $users
);

Arrow functions, a.k.a. short closures.

class A
{
    public string $name;
    
    public ?Foo $foo;
}

Type properties. There's quite a lot to tellabout them.

$data['date'] ??= new DateTime();

The null coalescing assignment operator.

class ParentType {}
class ChildType extends ParentType {}

class A
{
    public function covariantReturnTypes(): ParentType
    { /* … */ }
}

class B extends A
{
    public function covariantReturnTypes(): ChildType
    { /* … */ }
}

Improved type variance. If you're not sure what that's about, you should take a look at this post about Liskov and type safety .

$result = [...$arrayA, ...$arrayB];

The array spread operator.

$formattedNumber = 107_925_284.88;

The numeric literal separator.

[preloading]
opcache.preload=/path/to/project/preload.php

Preloading improves PHP performance across requests.

There's even more. If you want a full list, you can find iton this blog. What 7.4 feature do you like the most? Let me know on Twitter or viaemail.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK