1

The below code is used with queries. console_log is a function that prints PHP output to the console output, I took it from the internet. However, I don't think these are the problems.

    class Holder {
        public $name = "";
        public $uid = 0;
        public $wrs = 0;
    }
    function getWRForMapRun($mapid, $runid) {
        ...
        $user = new Holder();
        ...
        $user->$uid = $uid;
        $user->$name = $name;
        console_log("uid " . $uid . " name " . $name); // correct
        return $user;
    }
    function returnWRersMaps() {
        ...
        $userr = new Holder();
        console_log("mapid " . $row['mapid'] . " runid " . $row['runid']); // valid values
        $userr = getWRForMapRun($row['mapid'], $row['runid']);
        console_log("id " . $userr->$uid . " name ". $userr->$name); // empty 
    }

The problem with the code above is that at the last console log values are empty. It prints something like this: id name . Not even 0, as one would expect if there was a typical bug. I checked and values match when getWRForMapRun is called. (called in the correct order, and that console log provides the correct information.

It is among my first times using PHP, so it might be the object oriented part that's causing this.

Any clues to what needs to be done to receive the correct values?

EDIT: Using PHP 7

EDIT2: Edited first console_log to reflect reality - sorry!

Eric
  • 140
  • 14
  • Are you sure this is your code? There shouldn't be dollars $ after the arrows -> – NineBerry Apr 04 '21 at 11:38
  • You're correct, I modified it last minute and wrote ```$user->$uid``` instead of ```$uid``` not thinking anything of it. Please post this as an answer – Eric Apr 04 '21 at 11:41
  • You need to use `$user->uid` instead of `$user->$uid` in order to read the uid property of the object – NineBerry Apr 04 '21 at 11:44
  • I understood that and it works now – Eric Apr 04 '21 at 11:44
  • Does this answer your question? [What does the PHP syntax $var1->$var2 mean?](https://stackoverflow.com/questions/2316370/what-does-the-php-syntax-var1-var2-mean) – NineBerry Apr 04 '21 at 11:48

0 Answers0