Home >Backend Development >PHP Tutorial >Graphic and text analysis: Is the assignment operation of PHP objects 'passing by value' or 'passing by address'?

Graphic and text analysis: Is the assignment operation of PHP objects 'passing by value' or 'passing by address'?

不言
不言Original
2018-04-20 11:48:511731browse

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 = &#39;hello world&#39;;



$b = $a;



$a = &#39;bey world&#39;;







var_dump($a);



var_dump($b);



?>



At this time, $b = $a, the storage in the memory is roughly like this:


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.
Also give a simple example:

 <br>
<?php
class Person
{
    public $name;
    public $age;
}
 
$a = new Person();
$b = $a;
$c = &$a;
$a->name = &#39;gavin&#39;;
$a = null;
 
var_dump($b);
var_dump($a);
var_dump($c);

Execution result:

object(Person)#1 (2) { ["name"]=> string(5) "gavin" ["age"]=> NULL } 

NULL 

NULL



The operation process of the variables in the above example is roughly as follows :

$b = $a;

$c =& $a;



When an object instance is assigned to a new variable, the new variable will access the same instance.
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.


$a = null;




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:

$f = new Person();

$b = clone $f; //创建一个对象副本




##Through this article, we have shared the assignment operation of objects in PHP. What is passed is the object address instead of copying the object. You can use the clone operator in PHP to copy objects. My ability is limited. If you find any "bugs" in this article, please feel free to give me some advice. At the same time, if you have other questions or insights, please feel free to share them. We learn together, communicate together and make progress together.

Related recommendations:


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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn