0

I've been toying with a RuneScape hiscore image creator. People tend to use these for signatures on their profiles on forums.

While reworking some of the code, I broke it. I'm getting:

Undefined variable: exp in /assets/user.php on line 8 Fatal error: Cannot access empty property in /assets/user.php on line 8

However, the variable isn't even on line 8 (I assume that's something to do with it trimming white space).

Here is my User.php file

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

I was getting errors with the $names but that was because I wasn't using global $names in the functions.

Jesse
  • 13
  • 5

1 Answers1

2

You're using the wrong syntax.

Use $this->variableName not $this->$variableName

i.e. change return $this->$exp[0]; to return $this->exp[0];

Daniel
  • 1,229
  • 14
  • 24
  • I'm now getting undefined index errors for index 0 for my array(s). Though they should be set in the constructor, what'd be a good way to see they're being set? – Jesse Nov 11 '16 at 21:10
  • After you create an instance of the User class, use the getter methods to see if anything is set. – Daniel Nov 11 '16 at 21:13
  • It's showing it's not set. How should I be setting the variables within the constructor so they work with the instance of the class? Right now the instance is: `$user = new User($result);`, and mySkills/exp should be set in the constructor, but are not. I added `echo 'Setting ' . $id . ' to ' . $array[$arrId]` to the constructor and nothing is displaying on the page. – Jesse Nov 11 '16 at 21:25
  • Did you change all of the wrong syntax? It's also wrong within the `while()` loop, not just the getter methods. – Daniel Nov 11 '16 at 21:47
  • Yes, I've actually got it down to another error. The problem with it being set was the `count($result)` should have used `$array`. Now I'm getting "A non well formed numeric value encountered" because `$temp = explode(" ", $array[$arrId++]);` isn't setting the temp[0] correctly. I used an echo to see what it's being set as, and it's taking both values, I.E: `447663457 35892` when it should only have `447663457` – Jesse Nov 11 '16 at 21:57
  • I've finally fixed all the errors. Ended up using `preg_split` instead of explode. – Jesse Nov 11 '16 at 23:03