Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Syntax error while defining an array as a property of a class

Tags:

object

php

...

public $aSettings = array(
  'BindHost' => "127.0.0.1",
  'Port' => 9123,
  'MaxFileSize' => (5 * (1024 * 1024)), // unexpected "(" here
  'UploadedURL' => "http://localhost",
  'UploadPath' => dirname(__FILE__) . "/upload",
  'UploadMap' => dirname(__FILE__) . "/uploads.object",
  'RegisterMode' => false
);

...

This is my code, straight from a class. The problem I have is the "unexpected ( on line 22", line 22 being MaxFileSize.

I can't see a problem with it, is this a Zend Engine limitation? Or am I blind.

like image 966
Blake Avatar asked Feb 10 '12 09:02

Blake


People also ask

Why do I get syntax errors when defining a function?

You might run into invalid syntax in Python when you’re defining or calling functions. For example, you’ll see a SyntaxError if you use a semicolon instead of a colon at the end of a function definition: >>>. >>> def fun(); File "<stdin>", line 1 def fun(); ^ SyntaxError: invalid syntax.

Why do I get a SyntaxError when passing a value?

When you attempt to assign a value to pass, or when you attempt to define a new function called pass, you’ll get a SyntaxError and see the "invalid syntax" message again. It might be a little harder to solve this type of invalid syntax in Python code because the code looks fine from the outside.

Why is my syntax not valid in Python?

Often, the cause of invalid syntax in Python code is a missed or mismatched closing parenthesis, bracket, or quote. These can be hard to spot in very long lines of nested parentheses or longer multi-line blocks.

What is a SyntaxError in Python?

The other type of SyntaxError is the TabError, which you’ll see whenever there’s a line that contains either tabs or spaces for its indentation, while the rest of the file contains the other. This might go hidden until Python points it out to you!


1 Answers

You cannot use non-constant values while initializing class properties in PHP versions earlier than 5.6.
These are initialized at compile time, at which PHP will do no calculations or execute any code. (5 * (1024 * 1024)) is an expression that requires evaluation, which you cannot do there. Either replace that with the constant value 5242880 or do the calculation in __construct.

PHP 5.6, introduced in 2014, allows "constant scalar expressions" wherein a scalar constant or class property can be initialized by an evaluated expression in the class definition rather than the constructor.

like image 195
deceze Avatar answered Sep 28 '22 19:09

deceze