Parse error: syntax error, unexpected T

Class Config{
public $levels            = 10;
public $points_difference = 100;
public $diff_level        = 3;
public $timer_seconds     = 60;
public $maxBonus          = 0;
public $maxScore          = 0;
public $maxTotalScore     = 0;
public $pointsLevel       = $this->points_difference * $this->diff_level;
}

I get Parse error: syntax error, unexpected T_VARIABLE error on the last line. Any thoughts?


You can't use $this keyword during initialisation.

You need to use a constructor if you need so.

Class Config{
    public $levels            = 10;
    public $points_difference = 100;
    public $diff_level        = 3;
    public $timer_seconds     = 60;
    public $maxBonus          = 0;
    public $maxScore          = 0;
    public $maxTotalScore     = 0;

    public $pointsLevel; //no initialisation here

    function __construct() {
          $this->pointsLevel       = $this->points_difference * $this->diff_level;
    }
}

You cannot use $this on property's
Using from following solution:

<?php
class Config
{

    public $levels            = 10;
    public $points_difference = 100;
    public $diff_level        = 3;
    public $timer_seconds     = 60;
    public $maxBonus          = 0;
    public $maxScore          = 0;
    public $maxTotalScore     = 0;
    public $pointsLevel;

    public function __construct()
    {
        $this->$pointsLevel = $this->points_difference * $this->diff_level;
    }
}
链接地址: http://www.djcxy.com/p/69604.html

上一篇: 在PHP代码意外的T中解析错误

下一篇: 解析错误:语法错误,意外的T