PHP私有变量是'undefined'虽然它是有定义的


PHP private variable is 'undefined' though it is defined

我一直在玩RuneScape的hiscore图像创建器。人们倾向于在论坛的个人资料中使用这些签名。

在重写一些代码时,我破坏了它。我:

未定义变量:exp在/assets/user.php的第8行致命错误:无法访问/assets/user.php第8行

中的空属性

但是,变量甚至不在第8行(我假设这与它修剪空白有关)。

这是我的User.php文件

<?php
class User {
public static $names = ["Overall", "Attack", "Defence", "Strength", "Hitpoints", "Ranged", "Prayer", "Magic", "Cooking",
"Woodcutting", "Fletching", "Fishing", "Firemaking", "Crafting", "Smithing", "Mining","Herblore", "Agility", "Thieving", "Slayer",
"Farming", "Runecrafting", "Hunter", "Construction", "Summoning","Dungeoneering", "Divination", "Invention"];
private $user;
private $mySkills = [];
private $exp = [];
public function __construct($result) {
    $array = explode(",", $result);
    $id = 0;
    $arrId = 1;
    while($arrId < count($result)) {
        $this->$mySkills[$id] = $array[$arrId++];
        $temp = explode(" ", $array[$arrId++]);
        $this->$exp[$id++] = $array[0];
    }
}
public function getTotalXp() {
    return $this->$exp[0];
}
public function getLevel($skill) {
    global $names;
    for ($x = 0; $x <= count($names); $x++) {
        if(strcasecmp($skill, $names[$x]) == 0) {
            return $this->$mySkills[$x];
        }
    }
    return 0;
}
public function getExp($skill) {
    global $names;
    for ($x = 0; $x <= count($names); $x++) {
        if(strcasecmp($skill, $names[$x]) == 0) {
            return $this->$exp[$x];
        }
    }
    return 0;
}
}
?>

我得到错误的$名称,但那是因为我没有使用全局$名称的函数。

你用错了语法。

使用$this->variableName 而不是 $this->$variableName

。change return $this->$exp[0]; to return $this->exp[0];