Search code examples
phpyii2php-7.0

Array in the left part of equating statement. " Unexpected '='" error


I try to run Yii2 app on apache server with 7.0 php version. Yii2 framework package version:

yiisoft/yii2                        3.0.x-dev 9f215f3

So, when I run my site.loc index page in the browser, I get an error

Parse error: syntax error, unexpected '=' in /var/www/html/remi-web.co/vendor/yiisoft/yii2/di/Container.php on line 365

The piece of code which cause an error:

 /* @var $reflection ReflectionClass */
    [$reflection, $dependencies] = $this->getDependencies($class);

An array[] in the left side of equing statement. I have been never faced with this kind of statement before, so I don't even know where to look for explaining.

I have just install php7.0 on Apache so maybe it needs some module installed or some property enable in settings to run it properly.

There is part of phpinfo details:

PHP Version 7.0.33-23+ubuntu18.04.1+deb.sury.org+1
Loaded Modules  core mod_so mod_watchdog http_core mod_log_config mod_logio mod_version mod_unixd mod_access_compat mod_alias mod_auth_basic mod_authn_core mod_authn_file mod_authz_core mod_authz_host mod_authz_user mod_autoindex mod_deflate mod_dir mod_env mod_filter mod_mime prefork mod_negotiation mod_php7 mod_reqtimeout mod_rewrite mod_setenvif mod_status 

It works on production server with PHP Version 7.0.32, but doesn't work on my local machine.


Solution

  • Since PHP 7.1 we have destructuring assigment of arrays.

    [$x,$y] = [1,2];
    echo $x;  // gives 1
    

    This is shorthand for list():

    list($x,$y) = [1,2];
    echo $x; // gives 1
    

    You can read about that PHP 7.1 feature here.