Understand the variable assignment of PHP principle in simple terms

Time:2022-1-14

Variable assignment in PHP

It is estimated that many people will despise this title, variable assignment? excuse me? We’ll learn the first lesson of development, okay. However, such basic things will make many people circle, such as the relationship between value and reference. Today, let’s talk about it in detail.

First, define variables and assign values, needless to say


$a = 1;

$b = '2';

$c = [4, 5, 6];

$d = new stdClass();

Four variables, which define the objects of integer, string and array respectively. These are also the four types we have to deal with every day.

Then, assign a value to the variable.


$a1 = $a;

$b1 = $b;

$c1 = $c;

$d1 = $d;

Please note that the first three assignments are normal assignments, that is, copies of specific contents. When we modify $A1, $a will not change$ A1 is the newly opened memory space, which holds our value. In other words, their values are the same, but the memory addresses are different. It’s just two people who look alike.

But $D1 and $D are not. The values of these two goods are not only the same, but also the memory address is the same. This is what we call reference assignment. When $D1 changes, $D2 also changes.

It can be said that reference assignment is to establish a shortcut under windows or a soft link in Linux for the original variable.

To illustrate with specific examples, the first is the assignment of ordinary values:

//Normal assignment

$v = '1';

$c = $v;

$c = '2';

echo $v, PHP_EOL; // '1'

//Arrays are also normal assignments

$arr1 = [1,2,3];

$arr2 = $arr1;

$arr2[1] = 5;

print_r($arr1); // [1, 2, 3]

$C has no effect on the value of $v$ Arr2 modifies the subscript 1, that is, the second number is 5. Of course, it will not affect $Arr1.

What about reference assignment in object form?

//Objects are all reference assignments

class A {

    Public $name = 'I am a';

}

$a = new A();

$b = $a;

echo $a->name, PHP_ EOL; // ' I'm a '

echo $b->name, PHP_ EOL; // ' I'm a '

$B - > name = 'I'm B';

echo $a->name, PHP_ EOL; // ' I'm B '

Sure enough, after $B modifies the content of the name attribute, the name in $a becomes the content modified by $B.

In this case, if the object does not want to be passed by reference, one is to use__ Clone (), that is, the prototype pattern to make your own copy. Second, start a new one from the outside.

//Use cloning to solve reference passing problems

class Child{

    Public $name = 'I'm a subordinate of A1';

}

class A1 {

    Public $name = 'I am a';

    public $child;

    function __construct(){

        $this->child = new Child();

    }

    function __clone(){

        $this->name = $this->name;

        //New or clone with child

        // $this->child = new Child();

        $this->child = clone $this->child;

    }

}

a1 = new A1();

echo $a1->name, PHP_ EOL; //  Output A1 original content

echo $a1->child->name, PHP_EOL;

$b1 = $a1;

echo $b1->name, PHP_ EOL; //  B1 is now also the content of A1

echo $b1->child->name, PHP_EOL;

$B1 - > name = 'I'm B1'// B1 modification

$B1 - > child - > name = 'I'm a subordinate of B1';

echo $a1->name, PHP_ EOL; //  A1 becomes the content of B1

echo $a1->child->name, PHP_EOL;

//Use__ clone

$b2 = clone $b1; //  B2 clone B1

$B2 - > name = 'I'm B2'// B2 modification

$B2 - > child - > name = 'I'm a subordinate of B2';

echo $b1->name, PHP_ EOL; //  B1 will not become the content modified by B2

echo $b1->child->name, PHP_EOL;

echo $b2->name, PHP_ EOL; //  There is no problem with the content modified in B2. B1 and B2 are not the same goods

echo $b2->child->name, PHP_EOL;

The reference of the object is really easy to be encircled. Especially for more complex objects, internal attributes and various references to other objects. In this case, you must carefully confirm whether reference assignment will cause problems. If there are problems, you can use new objects or cloning technology to deal with reference problems.

Finally, take it easy. The assignment of reference variables is the same as we pass reference parameters to methods. Just use a & symbol!

//Reference assignment

$b = &$v;

$b = '3';

echo $v, PHP_EOL;

Today, we have a more in-depth study and understanding of assignment in PHP, especially ordinary assignment and reference assignment. The next time you look at the code and framework, you can pay attention to how others use these two assignments flexibly. Ha, you can also try whether you can use these two methods to transform the bug you have written!

Test code:

https://github.com/zhangyue0503/dev-blog/blob/master/php/201910/source/PHP%E7%9A%84%E5%8F%98%E9%87%8F%E8%B5%8B%E5%80%BC.php

Reference documents:

https://www.php.net/manual/zh/language.variables.basics.php

This is the end of this article about understanding the variable assignment of PHP principle in simple terms. For more information about PHP variable assignment, please search the previous articles of developeppaer or continue to browse the relevant articles below. I hope you will support developeppaer in the future!