解析错误:语法错误,意外T_VARIABLE - 公共变量

时间:2014-04-25 06:39:37

标签: 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       = $this->points_difference * $this->diff_level;
}

我得到Parse错误:语法错误,最后一行出现意外的T_VARIABLE错误。 有什么想法吗?

2 个答案:

答案 0 :(得分:1)

在初始化期间,您无法使用$this关键字。

如果需要,您需要使用构造函数。

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;
    }
}

答案 1 :(得分:0)

您不能在财产的上使用$this 使用以下解决方案:

<?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;
    }
}