I’m working on a login class in PHP. I have the following line inside a class definition:
private $salty = sha1('salty');
…which is giving me the following error:
“Parse error: parse error, expecting ','' or‘;” in C:\xampp\htdocs\test\includes\jaLogin.php on line 26″
I’ve tried using this line outside of the class definition and it works fine, it’s only when I use it inside the class definition that I get a problem.
If I remove the word “private” I get a slightly different error:
Parse error: parse error, expecting `T_FUNCTION’ in C:\xampp\htdocs\test\includes\jaLogin.php on line 26
I feel like I’m missing something obvious…
You can’t use function-calls to initialize class member variables.
is the one way to initialize your variable.
EDIT
Even, e.g., a simple concatenation of two constant strings is not allowed (
protected $_string = 'Hello ' . 'World!';). The evaluation of class properties happens at compile time, so the usage of constructs that depend on run-time information is illegal.(Properties)