1

I am attempting to fit an associative array declaration containing strings into a column width of 80 (style guide). This is being done in an abstract class. To fit within the 80 column width I am using the concatenation operator for php. See the following code snippet.

Original code format:

abstract class VideoBase
{
  protected $config_types =
    array('production' =>'Configures all hardware for production runs',
          'development'=>'Configures project specific development connections if available. Otherwise, only out the window and heads down connections are made');
  function __construct()        
  {
    print_r($config_types);
  }    
  function __destruct()
  {
  }
}

Attempted code format:

abstract class VideoBase
{
  protected $config_types =
    array('production' =>'Configures all hardware for production runs',
          'development'=>'Configures project specific development connections '.
                         'if available. Otherwise, only out the window and '.
                         'heads down connections are made');
  function __construct()        
  {
    print_r($config_types);
  }    
  function __destruct()
  {
  }
}

I receive the following error: PHP Parse error: syntax error, unexpected '.', expecting ')'

To my knowledge the above syntax is correct. The parse error only occurs when the associative array is declared inside the abstract class.

What am I doing incorrectly preventing this syntax from working?

Answer: As stated in the accepted answer below, the parsing error is due to the PHP Parser version not knowing how to handle the syntax when done in this manner. I need PHP version 5.6 or greater for this to work.

Kableka
  • 53
  • 3
  • try wrap string value into ( ) like 'develop'=>('xx'.'aaa') – daremachine Oct 19 '18 at 19:17
  • Both of the arrays will look exact the same. You need to insert a new line of some kind in the string, like a `
    ` tag or a `\r\n`. https://3v4l.org/2DBdq
    – Andreas Oct 19 '18 at 19:24
  • 1
    What version of PHP do you use? PHP 5.6 and 7.x accept your code format just fine. 5.5 and before display the error you mention. See https://3v4l.org/ZjnH3. Any chance you can update your environment to a more recent version? – jh1711 Oct 19 '18 at 19:28
  • I attempted wrapping it in () and it did not work. The goal is for both the arrays to look the exact same in terms of the string stored. The problem is trying to store that string within the 80 column limit. The PHP version is 5.3.3. I will check with the system admins to see if it would be possible to update the PHP version. Thanks! – Kableka Oct 19 '18 at 19:37
  • The code works fine for me even in PHPv 4.49 [Sandbox](http://sandbox.onlinephpfunctions.com/code/09215261c47976841a27d4bf48e2921150c72038) – ArtisticPhoenix Oct 19 '18 at 19:46
  • 1
    Are you sure those are actual `.` and not something that looks like a `.` such as a non-utf8 char? Was the code pasted from MsWord or something funky... I just wonder as I have never seen that happen, in almost 9 years of PHP programing... I guess it's not impossible, just I never seen it. – ArtisticPhoenix Oct 19 '18 at 19:50
  • it is indeed a standard keyboard period character. I am able to use it all over the place in the code to concatenate strings with variables for printouts and the like. I wrapped it in an abstract class and added the protected field to the variable declaration in the sandbox. http://sandbox.onlinephpfunctions.com/code/0cb9a9e297ec1e660f038df2570d4e96aa2c57b5 The code can be parsed correctly as early as of version 5.6.2. Anything older and it gives a parsing error. I believe this boils down to php versioning. Thanks for the help! – Kableka Oct 19 '18 at 21:21

2 Answers2

1

Prior to 5.6 you cannot declare anything other than a scalar value as a class property.

No function calls, no arithmetic, and no concatenation.

If neither upgrading nor a simple violation of your code style guide is an option then move that value assignment to the constructor.

Ref: http://php.net/manual/en/migration56.new-features.php#migration56.new-features.const-scalar-exprs

Sammitch
  • 30,782
  • 7
  • 50
  • 77
0

This works for me in PHP 5.6+:

protected $config_types =                                                              
  array('production' =>'Configures all hardware for production runs',        
        'development'=>'Configures project specific development'
                       .' connections if available. Otherwise, only out'
                       .' the window and heads down connections are'
                       .' made');

However, I'd discourage that -- it's hard to maintain. Rather, consider using a heredoc, which gives you the most columns to work with:

$tmp_d = preg_replace("/\r|\n/", '', <<<EOTXT
Configures project specific development connections if available. 
Otherwise, only out the window and heads down connections are made
EOTXT
);                        
protected $config_types =                                  
  array('production' =>'Configures all hardware for production runs',
        'development'=>$tmp_d);                        

You might also consider using [] array notation, instead of array ().

Finally, you might consider putting these strings into a message catalog (i.e., an external file without such style guidelines) then loading that catalog and filling its contents in here. You might already have such a catalog if you're localizing.

bishop
  • 37,830
  • 11
  • 104
  • 139