0

This is the class that I have created in PHP

class userinfo
    {
        public $username;
        public $totalscore;
        public $userid;
    }

The code below is in a finite loop, and i is set to 0 before entering the loop. And the variable user_array is defined to be an array using the following code:

$user_array = array();

(some code here...)

    $i++;
    $user_array[i] = new userinfo();
    $user_array[i]->totalscore = $stattotal;
    $user_array[i]->userid = $id;

For some reason I cant understand why this wont work. I need to create an array of objects. And each object must hold three variables. How do I go about doing so ?

Thank you in Adv. for your Help !

4
  • What do you get when you var_dump you array? Commented Apr 26, 2013 at 9:59
  • 1
    It should be $i in your object array, $user_array[$i]. Commented Apr 26, 2013 at 10:00
  • 1
    Pro tip: put error_reporting(E_ALL) at the beginning of your script, and never ignore any warning shown. Commented Apr 26, 2013 at 10:01
  • so is the code correct if I do the modification ? Like is the logic correct ? Also can i assign the object variables the way I have mentioned above ? $user_array[$i]->totalscore=$stattotal; Commented Apr 26, 2013 at 10:03

3 Answers 3

1

Worked fine for me, remember the $ when using variables.

http://phpfiddle.org/main/code/muv-yx6

Sign up to request clarification or add additional context in comments.

Comments

1

You must have a dollar sign ($i) before all variables in PHP.

Comments

0

You can use get_class_vars method to get all properties of class

$my_class = new myclass();

$class_vars = get_class_vars(get_class($my_class));

foreach ($class_vars as $name => $value) {
    echo "$name : $value\n";
}

SOURCE : http://php.net/manual/en/function.get-class-vars.php

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.