Variables Modified Using TeamSpeak API for PHP

I am developing a tool for a website and I have a strange problem or, better yet, a strange situation.

I use the following code to retrieve data from a TeamSpeak server. I use this information to create a profile for the user.

$ts3 = TeamSpeak3::factory("serverquery://dadada: dadada@dadada :1234/"); // Get the clients list $a=$ts3->clientList(); // Get the groups list $b=$ts3->ServerGroupList(); // Get the channels list $c=$ts3->channelList(); 

Now the strange situation is that the output of this code block is:

 // Get the clients list $a=$ts3->clientList(); // Get the groups list $b=$ts3->ServerGroupList(); // Get the channels list $c=$ts3->channelList(); echo "<pre>";print_r($a);die(); 

(Pay attention to print_r )
Completely different from the output of this code block:

 // Get the clients list $a=$ts3->clientList(); // Get the groups list #$b=$ts3->ServerGroupList(); // Get the channels list #$c=$ts3->channelList(); echo "<pre>";print_r($a);die(); 

What I mean is that the functions that I call after clientList() (which clientList() one stored in the variable $a ) change this content of the variable. This means that they added their output to the variable.

I have never studied PHP professionally, I'm just trying to do it ... Am I missing something about this language that justifies this behavior? If so, what can I do to stop him?

Thanks to everyone.

+5
source share
1 answer

You see parts of the "Object" in object-oriented programming

$ts3 represents an object containing all the necessary information, as well as some methods (or functions) that allow you to receive data from the object. Some of these methods will do different things to the object itself in order to get the additional data needed for a particular method call.

Consider the following simple object:

  • bike
    • Color
    • gears
    • __construct function ($ color, $ gears)
    • this.color = $color; this.gears = $gears
    • update function ()
    • this.headlight = true; this.gears = 10;

Now, when you first create it, it has only two properties:

 $myBike = new Bike('red',5); // $myBike.color = 'red'; // $myBike.gears = 5; 

... but as soon as you update, the properties have changed, and new ones added.

 $myBike->upgrade(); // $myBike.color = 'red'; // $myBike.gears = 10; // $myBike.headlight = true; 

Objects usually pass references rather than copy data to save memory.

... but if you want to make sure that you get a copy that does not change (i.e. does not use data references for the $ts3 ), clone the variable.

 $a = clone($ts3->clientList()); 

Be careful, this will effectively double the amount of memory and processor for this variable.

+1
source

Source: https://habr.com/ru/post/1236076/


All Articles