Is object assignment a reference in PHP?

Time:2022-1-5

In the previous article, we talked about the problem of variable assignment. One problem is that objects directly reference assignment when assigning variables. So what is the real situation?

Previous articles on variable assignment

Variable assignment in PHP

Object reference test

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

class SimpleClass
{}

$instance = new SimpleClass();

$assigned = $instance;
$reference = &$instance;

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

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

var_dump($instance);
var_dump($reference);
var_dump($assigned);
  • $instance is the simpleclass object after instantiation
  • $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 is naturally null
  • However, $assigned does not become null. It is still an instance object of simpleclass, and it has a VAR attribute

Isn’t it amazing? Normally, ordinary assignment is a copy, and the two variables won’t affect each other. The reference assignment is to copy the pointer (the same memory address). If you modify any variable, other variables will also change. But the ordinary assignment of objects does not seem to belong to any of them.

$reference is easy to understand. It is assigned with the & symbol, indicating that this variable is a reference assignment. It has become a shortcut to $instance, and it will change with all changes of $instance. This is variable level.

$assigned is a normal assignment from the literal point of view of the code. However, an object is a special form. The value assigned by it with ordinary assignment is actually a handle to the object. A note in the PHP manual describes this:

First, consider variables in PHP as data slots. This data slot can hold a basic type (int, string, bool, etc.). When creating a reference, what is stored in this slot is the memory address, or a pointer to the reference object. The reference has no copy operation, but only points the pointer to the original variable (reference data structure). When creating a normal assignment, it is the basic type of copy.

The object is different from the basic type. It cannot be saved directly in the data slot, but saves the “handle” of the object in the data slot. This handle is an identifier that points to a specific instance of the object. Although the handle is not the type we can intuitively operate on, 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 of the object. (note, not a reference! Not a reference! Not a reference!!). Through the handle, both variables can modify the same object. However, these two variables are not directly related. They are two independent variables. When one variable is modified to other values, it will 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.

Original note:
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 a variable level operation. It always saves only a value. When ordinary assignment, 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 ordinary assignment into reference assignment because it saves objects. The real reference assignment must be added with the & sign.

This content is a little around, but such content can better reflect their core competence. The meaning of reading a hundred times is self-evident. I can only understand a lot of knowledge in the manual by learning back and forth. The note author above writes very well. Good English friends can directly read the original English version.

Test 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 documents:
https://www.php.net/manual/zh/language.oop5.basic.php

Official account: hard core project manager

Add wechat / QQ friends: [xiaoyuezigonggong / 149844827] get free PHP and project management learning materials

Tiktok, official account, voice, headline search, hard core project manager.

Station B ID: 482780532