1

I can't figure out why I get this error:

Parse error: syntax error, unexpected '.', expecting ',' or ' in Settings.php on line 5

Here is the code

class Settings
{
    public $appDir = 'app';
    public  $controllersDir = $appDir . '/controllers';

}
Sirko
  • 72,589
  • 19
  • 149
  • 183
sanchosp
  • 13
  • 2

4 Answers4

2

Class property can't have variables in it.

class Settings
{
    public $appDir = 'app';
    public  $controllersDir = 'app/controllers';

}
xdazz
  • 158,678
  • 38
  • 247
  • 274
1

You can not execute arbitrary code in the class definition. You can only declare values (static strings, numbers and arrays with static information).

If you want to dynamically add values, you would have to do so in the constructor.

class Settings 
{
    public $appDir = 'app';
    public $controllerDir = '/controllers';

    public function __construct()
    {
        $this->controllerDir = $this->appDir . $this->controllerDir;    
    }    
}
Ikke
  • 99,403
  • 23
  • 97
  • 120
0

I believe that what you want is this

class Settings
{
    public $appDir = 'app';
    public  $controllersDir;


    function __construct(  ) {
        $this->$controllersDir = $this->appDir . '/controllers';
    }
}

If i'm not mistaken, you cannot have variable initialization that depends on another variables.

João Gonçalves
  • 3,903
  • 2
  • 22
  • 36
0

Anyway doing it like that is bad practice. Values should not be defined in the declaration... You should rather use a function getControllerDir returning the concatenation of two private properties.

PEM
  • 1,948
  • 14
  • 13