2016-11-11 104 views
0

我一直在玩弄一个RUNESCAPE hiscore形象的创造者。人们倾向于在论坛上使用这些签名来签名。PHP私有变量是“未定义”,虽然它被定义

而返工的一些代码,我把它弄坏了。我越来越:

Undefined variable: exp in /assets/user.php on line 8 Fatal error: Cannot access empty property in /assets/user.php on line 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; 
} 
} 
?> 

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

+1

'$此 - > $ mySkills'和'$此 - > $ exp'是__wrong__语法 –

+1

...写你对象的属性为:'$ this-> mySkills','$ this-> exp'等等 – SergeyLebedev

回答

2

您使用了错误的语法。

使用$this->variableName$this->$variableName

即改变return $this->$exp[0];return $this->exp[0];

+0

我现在正在为我的数组索引0获取未定义的索引错误。虽然它们应该在构造函数中设置,但是看到它们被设置的好方法是什么? – Jesse

+0

创建User类的实例后,使用getter方法查看是否设置了任何内容。 – Daniel

+0

它显示它没有设置。我应该如何在构造函数中设置变量,以便它们与类的实例一起工作?现在的实例是:'$ user = new User($ result);',和mySkills/exp应该在构造函数中设置,但不是。我加了'echo'Setting'。 $ id。 ' 至 ' 。 $ array [$ arrId]'给构造函数,页面上什么都没有显示。 – Jesse

相关问题