简体   繁体   中英

Creating Class objects inside a Class to a array var in PHP

I'm trying to add new PHP Objects to an Array inside a Class. It works for 2 entries but then after adding 1 more the whole array gets destructed.

public $players = array();

This is the Code where I add the new PHP Objects:

    /*
     * $data[0] => GameUserID
     * $data[1] => SlotID (In Game, INT)
     * $data[2] => GameUserName
     * $this => I've made a function called log($str) in the Main class, so I can
     * class this Function in the Player Class later
    */
    $this->log("Creating new player on SlotID #".$data[1]);
    $tmp = new Player($data[0], $data[2], $this);
    $this->players[$data[1]] = &$tmp;
    unset($tmp);

    //$this->players[$data[1]] = new Player($data[0], $data[2], $this);

The Class "Player" does not return anything on __construct().

I can't find the fail I make.

==============

I found out that the variable isn't saving after the function was called (reached the end). It's empty again whenever I call the function again.

You are passing an object to an array but then you're calling unset() on a variable that points to the object. Note that both variables point to the same object (even if you left out the & operator), so remove your call to unset() .

If you prefer, you can also immediately assign the object to your array:

 $this->players[$data[1]] = new Player($data[0], $data[2], $this);

This way, you don't have a $tmp variable in the way. The code isn't harder to understand, in my opinion – on the contrary, $tmp doesn't really express much.


Furthermore, I'm not sure where $data[1] is coming from or what it holds. If you are still having problems, please output all errors during development by putting error_reporting(E_ALL) at the top of your file. This can offer some very valuable insight as to what is going wrong.

As it got clear from the comments, the OP has only two values (states) in the $data[1] element. Let's call them State One and State Two .

This could result in

$this->players[State One] = new Player($data[0], $data[2], $this);
$this->players[State One] = new Player($data[0], $data[2], $this);
$this->players[State One] = new Player($data[0], $data[2], $this);
$this->players[State Two] = new Player($data[0], $data[2], $this);
$this->players[State Two] = new Player($data[0], $data[2], $this);
$this->players[State Two] = new Player($data[0], $data[2], $this);

Which will result in only 2 elements in the array, as each State One overrides the previous, and each State Two overrides the previous with the same key.

In this case, the solutions are either to create two properties for each state and populate them, or create a multidimensional array:

if ($data[1] === 'State One') {
    $this->playersFromStateOne[] = new Player($data[0], $data[2], $this);
} else {
    $this->playersFromStateTwo[] = new Player($data[0], $data[2], $this);
}

or

$this->players[$data[1]][] = new Player($data[0], $data[2], $this);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM