Home  >  Article  >  Backend Development  >  Do you really understand variable assignment in PHP?

Do you really understand variable assignment in PHP?

藏色散人
藏色散人forward
2021-09-19 16:42:203719browse

This title may be dismissed by many people. Variable assignment? excuse me? Let’s learn the first lesson of development, okay? However, such basic things can confuse many people, such as the relationship between values ​​and references. Today, let’s talk about it in detail.

First of all, it goes without saying much about defining variables and assigning values.

$a = 1;
$b = '2';
$c = [4, 5, 6];
$d = new stdClass();

The four variables define integer, string, and array objects respectively. These are also the four types we deal with every day.

Then, the variable assigns 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 content. When we modify $a1, $a will not change. $a1 is the newly opened memory space that holds our value. In other words, their values ​​are the same, but the memory addresses are different. They are just two people who look alike and have nothing to do with each other.

But $d1 and $d are not. Not only are the values ​​​​the same, but the memory addresses are also the same. This situation is what we call reference assignment. When $d1 changes, $d2 will also change.

It can be said: Reference assignment is to create a shortcut under Windows or a soft link in Linux for the original variable.

Use specific examples to illustrate. The first is the assignment of ordinary values:

// 普通赋值
$v = '1';
$c = $v;
$c = '2';
echo $v, PHP_EOL; // '1'

// 数组也是普通赋值
$arr1 = [1,2,3];
$arr2 = $arr1;
$arr2[1] = 5;
print_r($arr1); // [1, 2, 3]

$c will not affect the value of $v. $arr2 modifies subscript 1, that is, the second number is 5. Of course, it will not affect $arr1.

So what about reference assignment in object form?

// 对象都是引用赋值
class A {
    public $name = '我是A';
}

$a = new A();
$b = $a;

echo $a->name, PHP_EOL; // '我是A'
echo $b->name, PHP_EOL; // '我是A'

$b->name = '我是B';
echo $a->name, PHP_EOL; // '我是B'

As expected, after $b modified the content of the name attribute, the name in $a also changed to the content modified by $b.

In this case, if the object does not want to be passed by reference, one is to use __clone(), which is the prototype mode to make its own copy. The second is to create a new one from the outside.

// 使用克隆解决引用传递问题
class Child{
    public $name = '我是A1的下级';
}
class A1 {
    public $name = '我是A';
    public $child;

    function __construct(){
        $this->child = new Child();
    }

    function __clone(){
        $this->name = $this->name;
        // new 或者用Child的克隆都可以
        // $this->child = new Child();
        $this->child = clone $this->child;
    }
}

$a1 = new A1();

echo $a1->name, PHP_EOL; // 输出a1原始的内容
echo $a1->child->name, PHP_EOL;

$b1 = $a1;
echo $b1->name, PHP_EOL; // b1现在也是a1的内容
echo $b1->child->name, PHP_EOL;

$b1->name = '我是B1'; // b1修改内容
$b1->child->name = '我是B1的下级';
echo $a1->name, PHP_EOL; // a1变成b1的内容了
echo $a1->child->name, PHP_EOL;

// 使用__clone
$b2 = clone $b1; // b2克隆b1
$b2->name = '我是B2'; // b2修改内容
$b2->child->name = '我是B2的下级';
echo $b1->name, PHP_EOL; // b1不会变成b2修改的内容
echo $b1->child->name, PHP_EOL;
echo $b2->name, PHP_EOL; // b2修改的内容没问题,b1、b2不是一个货了
echo $b2->child->name, PHP_EOL;

The reference to objects can indeed easily confuse people. Especially for more complex objects, the internal properties have various references to other objects. In this case, you must carefully confirm whether reference assignment will cause problems. If there are problems, use new objects or cloning technology to deal with reference problems.

Finally, relax, the assignment of reference variables is the same as when we pass reference parameters to methods, just use an & symbol!

// 引用赋值
$b = &$v;
$b = '3';
echo $v, PHP_EOL;

Today we have a more in-depth study and understanding of the assignment issues in PHP, especially the issues of ordinary assignment and reference assignment. When you look at the code and framework next time, you can pay attention to how others use these two assignments flexibly. You can also try to see if you can use these two methods to fix the bugs you have written!

测试代码:
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
参考文档:
https://www.php.net/manual/zh/language.variables.basics.php

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of Do you really understand variable assignment in PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete