2010-11-24 5 views
5

Ich erhalte diese Fehlermeldung:Seltsame Parser-Fehler mit statischen verketteten String-Variable

Parse error: syntax error, unexpected '.', expecting ',' or ';' in /var/(...)/config.php on line 5

Mit diesem (vereinfacht) Code:

<?php 

class Config 
{ 
    public static $somevar = "Date: " . date('Y'); 
} 

?> 

Ich dachte, dies gilt php war, aber ich denke nicht ... was mache ich hier falsch? Vielen Dank!

Antwort

5

Nach the PHP docs:

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.

Versuchen

Config::$somevar = "Date: " . date('Y'); 

nach der Klassendefinition zu schreiben.

+1

Danke, wusste das nicht! –

1

Von Manual

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.

2

Kein Betrieb oder Funktion ist für Eigenschaft Initialisierung ermöglichen, da diese ausgewertet wird, wenn das Parsen.

Verwandte Themen