简体   繁体   中英

Objects in array odd behavior

first some code, then the problem

$squad_ids = $mysqli->query("SELECT id FROM squad WHERE owner=$owner_id");

include "squads.php";
$squads = array();
$squads_num = 0;

foreach ($squad_ids as $squad_id){
    $squads[$squads_num] = new Squad($squad_id["id"]);
    var_dump($squads[$squads_num]); // <-------------- DUMP 1
    $squads_num++;
}
...
foreach ($squads as $squad){
    var_dump($squad); // <-------------- DUMP 2
}

So what I do is to fill the $squads array with Squad objects (two at the tests). When dumping the array field ( DUMP 1 ) everything is just fine, both objects are shown.

After that I'm looping through the created $squads array and want to access the stored objects.

But for some odd reason it's both times the exactly same object. When dumping $squad ( DUMP 2 ) it shows exactly the same for both iterations in the test (it's the second objects both times).

I can't explain that.


(Actually there IS code between these two foreach loops, but it hasn't anything to do with $squads .)

For reference, the class:

class Squad {
    private $mysqli;
    private $squad = array(
        "id"=>null,
        "name"=>null,
        "owner"=>null,
        "battlefield"=>null,
        "posx"=>null,
        "posy"=>null,
        "radius"=>null,
        "action"=>null,
        "action_cmd"=>null
        );
    private $map = array();
    private $scan = array();

    function Squad($id){
        global $squad;
        global $mysqli;

        include "db_connect.php";

        $squad_request_raw = $mysqli->query("SELECT * FROM squad WHERE id = '$id'");
        $squad_request = $squad_request_raw->fetch_object();

        $squad["id"] = $squad_request->id;
        $squad["name"] = $squad_request->name;
        $squad["owner"] = $squad_request->owner;
        $squad["battlefield"] = $squad_request->battlefield;
        $squad["posx"] = $squad_request->posx;
        $squad["posy"] = $squad_request->posy;
        $squad["radius"] = $squad_request->radius;
        $squad["action"] = $squad_request->action;
        $squad["action_cmd"] = $squad_request->action_cmd;
    }

    function getId(){
        global $squad;
        return $squad["id"];
    }
}

You should definitely remove these lines:

global $squad;

From within your class. I can't set the code up to test at the moment, but that will definitely be causing strange things to occur... as that is the same var you are using in your foreach and in each instance of your class. All global vars in php work from the very base level of the code, so every instance will be using the same var... plus I don't see why your class requires this var to be global anyway.

As you are using var_dump immediately after each class instance is created in the first loop, they will look fine for the lifetime of each loop iteration. But because you are globalising the internal variable used by your class, each time an instance is created you will be modifying the same var/structure, especially as you aren't redefining the array in the constructor method. I actually don't think your class will even be using your private $this->squad array at all (although it may be globalising this, I can't tell without testing) . I bet if you always var_dump only the first item in the array — in your first foreach — you will see that it changes as the loop progresses. ie try this:

foreach ($squad_ids as $squad_id){
  $squads[$squads_num] = new Squad($squad_id["id"]);
  var_dump($squads[0]); // <-------------- DUMP 1
  $squads_num++;
}

You will probably see that $squads[0] will become the same as $squads[1] after $squads[1] has been created, and so on... Globalising the $squad var is effectively causing it to act the same as a &reference .

I don't see any issue in your code, but I would also try setting the key in your loop and see what's been printed:

foreach ($squads as $key => $squad){
    var_dump($squads[$key]); 
}

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