0

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.

whamsicore
  • 8,320
  • 9
  • 40
  • 50

2 Answers2

0

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.

richsage
  • 26,912
  • 8
  • 58
  • 65
  • Why not using the `__construct()` method instead? – Shoe Jan 26 '11 at 09:51
  • The Symfony docs mention using `preExecute` and `postExecute` - see http://www.symfony-project.org/gentle-introduction/1_4/en/06-Inside-the-Controller-Layer#chapter_06_sub_repeating_code_for_several_actions_of_a_module. `__construct()` should be fine as long as you call the `parent::__construct()` method too. I've only used `preExecute()` as it makes sense in the Symfony `executeMyActionName()` context. – richsage Jan 26 '11 at 09:59
0

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.

josemimg
  • 241
  • 2
  • 4