2010-11-24 30 views
5

chaîne concaténée statique Je reçois cette erreur:erreur d'analyse syntaxique étrange avec la variable

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

Avec ce (simplifié) Code:

<?php 

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

?> 

Je pensais que c'était valide php, mais je suppose que non ... qu'est-ce que je fais mal ici? Merci!

Répondre

5

Selon 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.

Essayez d'écrire

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

après la définition de la classe.

+1

Merci, je ne le savais pas! –

1

De 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

Aucune opération ou la fonction est de permettre à l'initialisation de la propriété, car cela est évalué lors de l'analyse.