PHP object oriented object value transfer method

 1 <?php 
 2     // Declare a variable and assign a value
 3     $a = 1;
 4     // Assign a value of data type to a variable
 5     $b = $a;
 6 
 7     // modify $a Value
 8     $a = 2;
 9     // $a and $b Two independent memory space modifications, one of which is unaffected
10     echo $b; // 1 
11 
12     class Person
13    {
14         public $name;
15         public $age;
16    }
17     // Assign data of object type to a variable
18     $p = new Person;
19     // By modifying the value of the attribute, we can determine the value transfer method of the object in the object-oriented
20     $p->name = 'jesse';
21     // If the value is modified successfully, it is the reference value 
22     echo $p->name; // jesse
23 ?>

Case explanation:

In this case, the value of $b will not be affected if the source value ($a's value) is modified. The memory relationship between $a and $b is as follows:

 

In code 20, the data type is assigned to the $p variable, and the internal value of the object is modified successfully through $P. comparing the value transfer method of the variable, it is proved that this is a reference value transfer. The memory relationship is as follows:

 

After these two kinds of basic understanding, we will deepen the way of object-oriented value transfer. Please look at the code:

 1 <?php 
 2     class Person
 3     {
 4         public $name;
 5         public $age;
 6     }
 7     // Assign data of object type to a variable
 8     $p = new Person;
 9     // By modifying the value of the attribute, we can determine the value transfer method of the object in the object-oriented
10     $p->name = 'jesse';
11     // Assign an object to another variable
12     $m = $p;
13     // Another variable modifies the property value
14     $m->name = 'Marry';
15     // Output the result of accessing internal properties by assigning variables to source variables
16     echo $p->name, $m->name;
17 ?>

Line 12 is the most "punchline" code of this article. The right side of the equal sign is an object, and the left side is a variable. So the sharp question is, $m is to copy the object? Or by reference? The code memory relationship is as follows:

 

Therefore, the property values of the last two objects are modified to Mary!!!

What is the situation of passing values by reference?

 

 1 <?php 
 2     class Person
 3     {
 4         public $name;
 5         public $age;
 6     }
 7     // Assign data of object type to a variable
 8     $p = new Person;
 9     // By modifying the value of the attribute, we can determine the value transfer method of the object in the object-oriented
10     $p->name = 'jesse';
11     // Assign an object to another variable
12     $m = &$p;
13     // Another variable modifies the property value
14     $m->name = 'Marry';
15     // Output the result of accessing internal properties by assigning variables to source variables
16     echo $p->name, $m->name;
17 ?>

 

                                     ------ Create By Jesse 2018.01.22

Tags: PHP Attribute

Posted on Sun, 03 May 2020 07:37:18 -0700 by waverider303