PHP 7.4 in 7 code blocks - stitcher.io
source link: https://www.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.
Scout APM helps PHP developers pinpoint N+1 queries, memory leaks & more so you can troubleshoot fast & get back to coding faster. Start your free 14-day trial today.
« back — written by Brent on May 23, 2020
PHP 7.4 in 7 code blocks
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. You can read about them in depth in this post.
class A
{
public string $name;
public ?Foo $foo;
}
Type properties. There's quite a lot to tell about them.
$data['date'] ??= new DateTime();
The null coalescing assignment operator. If you're unfamiliar with the null coalescing operator, you can read all about shorthand operators in this blog.
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. There are a few sidenotes to be made about them.
$formattedNumber = 107_925_284.88;
The numeric literal separator, which is only a visual aid.
[preloading]
opcache.preload=/path/to/project/preload.php
Preloading improves PHP performance across requests. It's a complicated topic, but I wrote about it here.
Noticed a tpyo? You can submit a PR to fix it.
If you want to stay up to date about what's happening on this blog, you can follow me on Twitter or subscribe to my newsletter:
Footnotes
New in PHP 7.4 — A comprehensive list of all things new in PHP 7.4
PHP 8 in 8 code blocks — The best features of PHP 8
Next up: PHP 8 in 8 code blocksFollow me: Twitter — RSS — Newsletter — Patreon — GitHub
© 2020 stitcher.io — Cookies & Privacy
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK