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.