Search code examples
phparraysparse-error

PHP parse error when using dot (.) inside Array


When I run the following code:

<?php
private $config = [
  'cacheFile' => 'a'.'b'
];

I receive:

Parse error: syntax error, unexpected '.', expecting ']' in ...

My config is: PHP Version 5.5.9-1ubuntu4.14, Server API FPM/FastCGI, nginx/1.4.6

I tested the code above also on localhost(OS X El Capitan) with Nginx/Apache and both tests passed ok.

Any idea where can be the problem? Thank you.


Solution

  • As per the docs:

    Status: Implemented in PHP 5.6

    This RFC brings static scalar expressions to the parser. This allows places that only take static values (const declarations, property declarations, function arguments, etc) to also be able to take static expressions.

    Therefore your 5.5 cannot do this.

    Note that only expressions which can be evaluated at compile time would work, so

    class foo {
       $x = 'a' . 'b'; // ok - can be calculated at compile-time
       $y = $_POST['foo']; // not ok - only calculable at runtime.
    }