Monsieur Zartosht

Monsieur Zartosht

Who am I?

A very simple blog that belongs to a very simple full-stack web developer about his daily life and struggles through coding. here you will find many things such as music, movies, and code (lots of codes!)

What I write about

Recent Posts

PHP 7.4 cool features

I was reading some news about PHP 7.4 and some of its features caught my eyes. first of all the "Short arrow functions" and in second "Spread Operator"! I always used these features in javascript and there wasn't a single time that I wished for that I had those in PHP as well.

Table of contents


    Short Arrow Functions

    $extended = function ($c) use ($callable, $factory) {
        return $callable($factory($c), $c);
    // with arrow function:
    $extended = fn($c) => $callable($factory($c), $c);

    In the past versions of PHP, we had to always used use word to import some variables that were outside the given scope! I hated this use word because it always made my code ugly! and if you suddenly had forgotten to use one variable it will break your code and how many times you missed & operator and pulled your hair and said what's wrong with my code?? why $foo variable won't change!!!

    $foo = "Zartosht";
    $extended = function ($c) use ($foo) {
        $foo = "{$foo} {$c}";
    echo $foo; // the result is 'Zartosht' but you expected 'Zartosht Sepideman'!

    In the following update in PHP 7.4 you can simply write:

    $foo = "Zartosht";
    $extended = fn($c) => {$foo = "{$foo} {$c}"};
    echo $foo; // the result is 'Zartosht Sepideman' as expected!

    Spread Operator

    $first = ['one', 'two'];
    $second = ['zero', ...$first, 'three'];
    // ['zero', 'one', 'two', 'three'];

    In earlier versions, we had two push items to an array and then reorder it to meet our needs, but now all we have to do is spread an array inside another array. let's say you have an array that has two members ('one' and 'two') then you want another array that has 'zero' and 'three' in addition to those members. you can simply pass the first array with '...' operator to the second array constructor and voila! there is your new array (['zero', 'one', 'two', 'three'])!

    Personally, I can't wait to update my PHP version :-D

    Posted in PHP , Web on by

    Share this post: