Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error: syntax error, unexpected '.', expecting ',' or ';' [closed]

Tags:

php

This thing is bugging me a lot. I'm getting Parse error: syntax error, unexpected '.', expecting ',' or ';' at this line

public static $user_table = TABLE_PREFIX . 'users';

TABLE_PREFIX is a constant created by define function

like image 679
Tejas Jadhav Avatar asked Jun 10 '12 14:06

Tejas Jadhav


People also ask

How do I fix parse error syntax error unexpected?

The best way to solve it is to remove the recently added plugins by disabling them. The WordPress site is also likely to generate an error after a code edit. A mistake as simple as a missing comma is enough to disrupt the function of a website.

What is parse error syntax error?

If the PHP code contains a syntax error, the PHP parser cannot interpret the code and stops working. For example, a syntax error can be a forgotten quotation mark, a missing semicolon at the end of a line, missing parenthesis, or extra characters.

What is syntax error unexpected?

Syntax Error – This error is caused by an error in the PHP structure when a character is missing or added that shouldn't be there. Unexpected – This means the code is missing a character and PHP reaches the end of the file without finding what it's looking for.


2 Answers

Static class properties are initialized at compile time. You cannot use a constant TABLE_PREFIX to concatenate with a string literal when initializing a static class property, since the constant's value is not known until runtime. Instead, initialize it in the constructor:

public static $user_table;

// Initialize it in the constructor 
public function __construct() {
  self::$user_table = TABLE_PREFIX . 'users';
}

// If you only plan to use it in static context rather than instance context 
// (won't call a constructor) initialize it in a static function instead 
public static function init() {
  self::$user_table = TABLE_PREFIX . 'users';
}

http://us2.php.net/manual/en/language.oop5.static.php

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

Update for PHP >= 5.6

PHP 5.6 brought limited support for expressions:

In PHP 5.6 and later, the same rules apply as const expressions: some limited expressions are possible, provided they can be evaluated at compile time.

like image 153
Michael Berkowski Avatar answered Sep 28 '22 11:09

Michael Berkowski


The dot is a string concatenation operator. It's a runtime function, so it can't be used to declare a static (parsetime) value.

like image 23
troelskn Avatar answered Sep 28 '22 12:09

troelskn