I read quite a few articles about how the variables are arranged inside the Zend machine and found out one interesting thing that I can not explain:

$int = 100; xdebug_debug_zval('int');/// int:(refcount=1, is_ref=0),int 100 $int = &$int; xdebug_debug_zval('int');/// int:(refcount=1, is_ref=1),int 100 

How is it that we create the link to itself? How is that even possible? I did not see anything like this, if you will put down cons, then please argue your choice.

Some information from what I know:

As a rule, is_ref=1 only if zval container is referenced by 2 or more variables by a hard link.

refcount is the number of variables referring to the same zval container, but the difference is that is_ref=0 works differently with is_ref=0 and is_ref=1 .

If is_ref=0 , and refcount > 1 then when creating hard links, a new zval container is created, if we make an assignment by value, then the new zval not created, but the old one is used.

If is_ref=1 , and refcount > 1 then when creating hard links, a new zval not created, but the old one is used, but if we do not create a hard link, we assign a new zval container by value.

PS I wrote this in order to show that I understand perfectly what I ask, and also show why the behavior of the code I wrote above is incomprehensible to me.

PS Fabulous

Note that if "refcount" is 1, then "is_ref" will always be equal to FALSE. http://php.net/manual/ru/features.gc.refcounting-basics.php

  • Comments are not intended for extended discussion; conversation moved to chat . - Nicolas Chabanovsky ♦

1 answer 1

Inside the zval structure, the name of the character that refers to it is not stored. This is easily seen from the definition of zval . The symbol itself (variable name) is stored separately in a symbol table.

Thus, what flags are marked inside zval , and what you call it are different entities. The name (symbol) for a variable is one thing. What is stored inside a variable, including all sorts of flags, is another.

Due to this, there may be many variables having the same name. For example, there may be a $int variable external to the function that has one value, and there may be a $int variable that is internal to the function, which has a different value, and there may be a variable of the same name referring to the same value as the previously mentioned $int variable.

This is implemented in the form of symbolic correspondence tables of symbols and zval values ​​for each scope.

In your example, the first character (or variable name) of $int is not the same as the second character of $int . Although they both refer to one zval at the moment when you look at them, at the time of creation the first $int was just a number, and the second was already a reference. The second one was already a link before you named it, assigning it to a $int character.

For example, below $a and $b differ from your two $int only by their name, which means that zval in PHP 7 will already have two links:

 $a = 100; xdebug_debug_zval('a'); // a: (refcount=0, is_ref=0)=100 $b = &$a; xdebug_debug_zval('b'); // b: (refcount=2, is_ref=1)=100 xdebug_debug_zval('a'); //a: (refcount=2, is_ref=1)=100 

For comparison, your version in PHP 7:

 $int = 100; xdebug_debug_zval('int'); // int: (refcount=0, is_ref=0)=100 $int = &$int; xdebug_debug_zval('int'); //int: (refcount=1, is_ref=1)=100 

Obviously, in PHP 7 a slightly different relationship to the initial value of the refcount for the simplest. For objects, everything was as it was:

 $int = new stdClass(); xdebug_debug_zval('int'); // int: (refcount=1, is_ref=0)=class stdClass { } $int = &$int; xdebug_debug_zval('int'); // int: (refcount=1, is_ref=1)=class stdClass { } 
  • To be honest, I did not understand anything! - MaximPro
  • I need an explanation for php 5.6, and your explanation for a newer version is MaximPro
  • Excuse me, but where does your question say that you need an explanation for PHP 5.6? You refer to the documentation for PHP 7, and you run tests in another version. Maybe this is the problem? - sanmai
  • What makes you think that I refer to the PHP 7.0 documentation? There it is just written for the old version! - MaximPro
  • I know that there is such a thing as a symbolic table of variables and for each scope it has its own, so the same variable names do not conflict with each other (there are also questions on the symbolic table, they also relate to the same topic, but I will ask later). But in my example, the same scope. I don’t understand when you wrote the 5th paragraph above (How is the first int number and the second link? I understand that there is an integer data type, but there is no data type) PS IMHO I don’t see any differences between the link and the variable point to the same zval container - MaximPro