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

不言
Release: 2023-03-24 13:52:01
Original
1642 people have browsed it

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);



?>
Copy after login


<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/>
Copy after login
<?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);
Copy after login

Execution result: <br/> <br/>

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

NULL 

NULL
Copy after login


<br/>The operation process of the variables in the above example is roughly as follows :

$b = $a;

$c =& $a;
Copy after login


<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;
Copy after login


<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; //创建一个对象副本
Copy after login




##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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!