Symfony:在操作中将常量声明为类属性的问题

This is my variable declaration inside my action: public $var = sfConfig::get('constant_name');

Returns php error: Parse error: syntax error, unexpected T_VARIABLE, expecting T_FUNCTION in C:\wamp\www\project\app\module\actions\actions.class.php on line 13

When I put $var = sfConfig::get('constant_name'); it works fine. The problem comes from using sfConfig::get() outside of a function inside of action class. Any idea why this is not working? Thanks.

If you want to have $var available across all methods in your actions class, try using the preExecute method in your actions class:

public function preExecute()
{
  $this->var = sfConfig::get("constant_name");
}

Then you'll be able to use $this->var in all methods in your action class to get the desired result. Initialising a class member should be done with a constant value eg "42" or "Foo" - you can't use the result of a function call for this.

http://www.php.net/manual/en/language.oop5.properties.php

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.