Monday, January 30, 2023
  • Events
  • Interviews
  • Jobs
  • Opinion
  • Whitepapers
  • Glossary
  • Community Forum
  • Web Hosting Directory
  • Login
  • Register
Cloud7 News
  • Cloud Computing
  • Web Hosting
  • Data Center
  • Linux
  • Cybersecurity
  • More
    • Network/Internet
    • Windows
    • Software
    • Hardware
    • Blockchain
    • Policy/Legislation
    • How-Tos
    • Troubleshooting
No Result
View All Result
Cloud7 News
  • Cloud Computing
  • Web Hosting
  • Data Center
  • Linux
  • Cybersecurity
  • More
    • Network/Internet
    • Windows
    • Software
    • Hardware
    • Blockchain
    • Policy/Legislation
    • How-Tos
    • Troubleshooting
No Result
View All Result
Cloud7 News
No Result
View All Result

Home > Development > What is new in PHP 7.4?

What is new in PHP 7.4?

The latest PHP 7 minor release, PHP 7.4 released to General Availability. At first glance, it is safe to say that PHP 7.4 brings a performance boost and improves code readability significantly. So let's take a quick look at what's new in PHP 7.4.


Erdem Yasar Erdem Yasar
September 3, 2019
3 min read
php-74-promo

Table of Contents

  • Spread Operator in Array Expression
  • Arrow Functions 2.0
  • Weak References
  • Preload
  • New Custom Object Serialization Mechanism

Spread Operator in Array Expression

Before PHP 7.4, it was possible to unpack an array or a Traversable with “…”, for example:

function test(...$args) { var_dump($args); }
test(1, 2, 3);

PHP 7.4 can apply this feature to array definitions too:

$arr = [...$args];

Being able to use Spread Operator in the array expressions both improves the performance and the code readability. Because Spread Operator provides better performance than the “array_merge” function. But also, Spread Operator can support traversable objects while the “array_merge” function can only support arrays. Here is an example:

$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
var_dump($fruits);

While these lines would cause and parse error in PHP 7.3, in PHP 7.4 it would return an array:

array(5) {
[0]=>
string(6) "banana"
[1]=>
string(6) "orange"
[2]=>
string(5) "apple"
[3]=>
string(4) "pear"
[4]=>
string(10) "watermelon"

It is also possible to expand the same array many times, and it’s also possible to use the Spread Operator in an array just like the normal elements. So it means these code will work as expected:

$arr1 = [1, 2, 3];
$arr2 = [4, 5, 6];
$arr3 = [...$arr1, ...$arr2];
$arr4 = [...$arr1, ...$arr3, 7, 8, 9];

You can also unpack the arrays returned by a function into a new array:

function buildArray(){
return ['red', 'green', 'blue'];
}
$arr1 = [...buildArray(), 'pink', 'violet', 'yellow'];

And the output will be:

array(6) {
[0]=>
string(3) "red"
[1]=>
string(5) "green"
[2]=>
string(4) "blue"
[3]=>
string(4) "pink"
[4]=>
string(6) "violet"
[5]=>
string(6) "yellow"
}

Generator syntax can also be used:

function generator() {
for ($i = 3; $i <= 5; $i++) { yield $i; } } $arr1 = [0, 1, 2, ...generator()];

Arrow Functions 2.0

The new arrow functions in PHP 7.4, it’s possible to rewrite the same code with fewer lines. For example:

function cube($n){ 
return ($n * $n * $n); 
} 
$a = [1, 2, 3, 4, 5]; 
$b = array_map('cube', $a); 
print_r($b);

It can be written as:

$a = [1, 2, 3, 4, 5]; $b = array_map(fn($n) => $n * $n * $n, $a);
print_r($b);

With PHP 7.4, the variable which is defined in a parent scope can implicitly be captured by value. So for example:

$factor = 10;
$calc = function($num) use($factor){
return $num * $factor;
};

It’s possible to write the function on a single line:

$factor = 10;
$calc = fn($num) => $num * $factor;

Weak References

With the new WeakReference class, you can retain a reference to an object which doesn’t prevent the object from being destroyed. Older versions of PHP support Weak References with extensions like pecl-weakref. Here is the example code from the proposal by Nikita Popov:

$object = new stdClass;
$weakRef = WeakReference::create($object);

var_dump($weakRef->get());
unset($object);
var_dump($weakRef->get());

First “var_dump” prints “object(stdClass)#1 (0) {}” but after the object is destroyed, second “var_dump” prints “NULL”.

Preload

One of the most important things PHP 7.4 brings us is the process of loading libraries and frameworks into the OPCache. This can bring a significant performance boost.

The preloading process is controlled by opchace.preload directive in the php.ini file. With this directive,  a PHP script is to be compiled and executed during the server booting. The downside of preloading is, preloaded files remain cached in opcache memory. A server restart is required for modifications to take effect.

New Custom Object Serialization Mechanism

Another proposal by Nikita Popo is considering “_sleep()” and “_wakeup()é magic methods and “Serializable” interface. Nikita claims these options can cause problems with complex and unreliable codes. New magic methods “_serialize()” and “_unserialize()” combines those two existing mechanisms.

You may be interested in: Top PHP frameworks 2020

See more Development News


Tags: PHP
Erdem Yasar

Erdem Yasar

Erdem Yasar is a news editor at Cloud7 News. Erdem started his career by writing video game reviews in 2007 for PC World magazine while he was studying computer engineering. In the following years, he focused on software development with various programming languages. After his graduation, he continued to work as an editor for several major tech-related websites and magazines. During the 2010s, Erdem Yasar shifted his focus to cloud computing, hosting, and data centers as they were becoming more popular topics in the tech industry. Erdem Yasar also worked with various industry-leading tech companies as a content creator by writing blog posts and other articles. Prior to his role at Cloud7 News, Erdem was the managing editor of T3 Magazine.

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

I agree to the Terms & Conditions and Privacy Policy.

Next Post
plesk-obsidian-feature

Plesk Obsidian: The 7 new features

Related News

GNOME 44 Alpha is out now

GNOME 44 Alpha is out now

January 27, 2023 4:20 pm
Google is shutting down its website optimization solution

Google is shutting down its website optimization solution

January 27, 2023 1:20 pm
DXVK 2.1 is now available, bringing HDR and HDR10 color space support

DXVK 2.1 is now available, bringing HDR and HDR10 color space support

January 26, 2023 5:00 pm
Freespire 9.0

Freespire 9.0 is now available for download

January 26, 2023 3:15 pm
Get free daily newsletters from Cloud7 News Get the Cloud7 Newsletter
Select list(s):

Check your inbox or spam folder to confirm your subscription.

By subscribing, you agree to our
Copyright Policy and Privacy Policy

Get the free newsletter

Subscribe to receive the latest IT business updates straight to your inbox.

Select list(s):

Check your inbox or spam folder to confirm your subscription.

Editor's Choice

What’s new in Linux kernel 6.2 rc5?

10 Best Web Hosting Services of 2023

Ubuntu 22.04 LTS is available for download. What is new?

CERN and Fermilab recommend AlmaLinux

7 best hosting control panels of 2023

How to update Linux Kernel without rebooting?

7 best Linux mail servers of 2023

7 best cPanel alternatives for 2023

7 best Linux web browsers for 2023

7 best CentOS alternatives

7 best Linux server distros of 2023

Interview with Igor Seletskiy on AlmaLinux

How to create a VM and install a Linux distro on VMware Workstation

Recent News

  • [Event] IT Forum CxO
  • Total public cloud revenues jumped by 21% in 2022
  • Pentagon supply chain fails basic cybersecurity requirements
  • Yugabyte releases YugabyteDB Voyager
  • Chinese 8220 Gang targets public clouds

Cloud7 News
Cloud7 is a news source that publishes the latest news, reviews, comparisons, opinions, and exclusive interviews to help tech users of high-experience levels in the IT industry.

EXPLORE

  • Web Hosting
  • Cloud Computing
  • Data Center
  • Cybersecurity
  • Linux
  • Network/Internet
  • Software
  • Hardware
  • How-Tos
  • Troubleshooting

RESOURCES

  • Events
  • Interviews
  • Jobs
  • Opinion
  • Whitepapers
  • Glossary
  • Community Forum
  • Web Hosting Directory

Get the Cloud7 Newsletter

Get FREE daily newsletters from Cloud7 delivering the latest news and reviews.

  • About
  • Privacy & Policy
  • Copyright Policy
  • Contact

© 2022, Cloud7 News. All rights reserved.

No Result
View All Result
  • Cloud Computing
  • Web Hosting
  • Data Center
  • Linux
  • Cybersecurity
  • More
    • Network/Internet
    • Windows
    • Software
    • Hardware
    • Blockchain
    • Policy/Legislation
    • How-Tos
    • Troubleshooting
  • Events
  • Interviews
  • Jobs
  • Opinion
  • Whitepapers
  • Glossary
  • Community Forum
  • Web Hosting Directory

© 2022, Cloud7 News. All rights reserved.

Welcome Back!

Sign In with Facebook
Sign In with Google
Sign In with Linked In
OR

Login to your account below

Forgotten Password? Sign Up

Create New Account!

Sign Up with Facebook
Sign Up with Google
Sign Up with Linked In
OR

Fill the forms below to register

*By registering into our website, you agree to the Terms & Conditions and Privacy Policy.
All fields are required. Log In

Retrieve your password

Please enter your username or email address to reset your password.

Log In
This website uses cookies. By continuing to use this website you are giving consent to cookies being used. Visit our Privacy and Cookie Policy.