The content of this article is about whether the assignment operation of PHP objects in graphic and text analysis is "passing by value" or "passing by address". It has a certain reference value. Now I share it with you. Friends in need can refer to it
We know that variables are always assigned by value by default. That is, when the value of an expression is assigned to a variable, the value of the entire original expression is assigned to the target variable. This means that when the value of one variable is assigned to another variable, changing the value of one variable will not affect the other variable.
Give a simple example:
<?php $a = 'hello world'; $b = $a; $a = 'bey world'; var_dump($a); var_dump($b); ?>
<br/>At this time, $b = $a, the storage in the memory is roughly like this: <br/>
<br/>But when an object is instantiated, the object is not passed by value, but the address of the object is passed. That is, the entire object is not copied, as doing so is time-consuming and memory intensive. <br/> Also give a simple example: <br/>
<br/>
<?php class Person { public $name; public $age; } $a = new Person(); $b = $a; $c = &$a; $a->name = 'gavin'; $a = null; var_dump($b); var_dump($a); var_dump($c);
Execution result: <br/> <br/>
object(Person)#1 (2) { ["name"]=> string(5) "gavin" ["age"]=> NULL } NULL NULL
<br/>The operation process of the variables in the above example is roughly as follows :
$b = $a; $c =& $a;
<br/>When an object instance is assigned to a new variable, the new variable will access the same instance. <br/>Reference assignment (&) means that the new variable refers to the original variable, in other words, becomes its "alias" or "pointer". Changing the new variable will affect the original variable and vice versa. <br/>
<br/>
$a = null;
<br/>
Maybe you will ask, since direct assignment does not copy the object, how can we copy the object? To clone a copy object in PHP, use the clone operator: <br/>
$f = new Person(); $b = clone $f; //创建一个对象副本
How to implement php object cloning
The above is the detailed content of Graphic and text analysis: Is the assignment operation of PHP objects 'passing by value' or 'passing by address'?. For more information, please follow other related articles on the PHP Chinese website!