Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Is object assignment a reference in PHP or not?

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "whether the object assignment is referenced in the PHP or not". In the daily operation, I believe that many people have doubts about whether the object assignment is referenced in the PHP. The editor consulted all kinds of data and sorted out a simple and easy-to-use method of operation. I hope it will be helpful for everyone to answer the doubt that "the object assignment is referenced in the PHP". Next, please follow the editor to study!

Is object assignment a reference in PHP or not?

In the previous article, we talked about variable assignment, and one of the problems is that when objects assign variables, they directly refer to them. So what is the real situation?

Previous articles on variable assignment

Variable assignment of PHP

Object reference test

After further studying the PHP manual, I found that the original object is really not a direct reference copy. Analyze through the examples in the following manual:

1class SimpleClass

2 {}

three

4$ instance = new SimpleClass ()

five

6$ assigned = $instance

7$ reference = & $instance

eight

9$ instance- > var ='$assigned will have this value'

ten

11$ instance = null; / / $instance and $reference become null

twelve

13var_dump ($instance)

14var_dump ($reference)

15var_dump ($assigned)

$instance is the instantiated SimpleClass object

$assigned direct assignment

$reference reference assignment

First, we define a variable var for the $instance object

Then assign $instance to null

For references, the $reference variable naturally becomes null.

But $assigned has not become null, it is still an instance object of SimpleClass, and highlight: it has the var property

Isn't it amazing? in theory, a normal assignment is a copy, and the two variables will not affect each other. While the reference assignment is to copy the pointer (the same memory address), modifying any one variable and other variables will also change. But the normal assignment of objects does not seem to belong to any of them.

Reference is easy to understand and is assigned using the & symbol itself, indicating that the variable is a reference assignment. It becomes a shortcut to $instance, and it changes everything that changes to $instance. This is at the variable level.

$assigned is literally a normal assignment. However, the object is a special form, and the value assigned by the ordinary assignment is actually a handle to the object. There is a Note in the PHP manual that describes it as follows:

First of all, think of the variables in PHP as data slots. This data slot can hold a basic type (int, string, bool, etc.). When creating a reference, the slot holds the memory address, or a pointer to the reference object, and the reference does not copy, but simply points the pointer to the original variable (reference data structure). When you create a normal assignment, it is the basic type of copy.

The object is different from the basic type, it cannot be stored directly in the data slot, but the "handle" of the object is saved in the data slot. This handle is an identifier that points to a specific instance of the object. Although a handle is not a type that we can manipulate intuitively, it is also a basic type.

When you get a variable that contains an object handle and assign it to another variable, the other variable gets the handle to that object. (note that it is not a quote! It's not a quote! It's not a quote! ). Through the handle, both variables can modify the same object. However, these two variables are not directly related, they are two independent variables, and when one variable is changed to another value, it does not affect the other variable. Only when this variable modifies the internal content of the object, the object content of another variable will change accordingly because it holds the same handle.

Note original text:

Https://www.php.net/manual/zh/language.oop5.basic.php#79856

Summary

Through the analysis of this paper, we can see that variable assignment is the operation at the variable level. It always holds just a value. When a normal assignment is made, this value is a basic type. When a reference is assigned, the saved basic type is a pointer. In any case, it will not directly convert a normal assignment to a reference assignment because it holds an object, and the real reference assignment must be appended with a & symbol.

This content is a bit roundabout, but this kind of content can better reflect their core competence. The meaning of reading a hundred times is self-evident, and I can only understand a lot of the knowledge in the manual by constantly learning back and forth. The above Note author is very good. Friends who are good at English can read the original English version directly.

Test the code:

Https://github.com/zhangyue0503/dev-blog/blob/master/php/201911/source/%E5%AF%B9%E8%B1%A1%E8%B5%8B%E5%80%BC%E5%9C%A8PHP%E4%B8%AD%E5%88%B0%E5%BA%95%E6%98%AF%E4%B8%8D%E6%98%AF%E5%BC%95%E7%94%A8%EF%BC%9F.php

Reference documentation:

Https://www.php.net/manual/zh/language.oop5.basic.php

At this point, the study on "whether the object assignment is a reference in PHP or not" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report