I read that such a record will display deprecated :

class A{} $obj = &new A; 

But I tried it on my host and did not see anything (although I included all errors and warnings). Not the point!

The question is: what is the difference between creating an object by reference and without it?

PS A very interesting thing turned out

 $obj = new A; xdebug_debug_zval('obj'); ///obj:(refcount=1, is_ref=0), object(A)[1] $obj = &new A; xdebug_debug_zval('obj'); ///obj:(refcount=1, is_ref=1), object(A)[1] 

I did not understand how is_ref=1 , when refcount=1 (usually is_ref=1 , when refcount>=2 )?

Another interesting point:

 class A{} $obj = &new A; xdebug_debug_zval('obj'); ///obj:(refcount=1, is_ref=1), object(A)[1] $alter = &$obj; xdebug_debug_zval('obj'); ///obj:(refcount=2, is_ref=1), object(A)[1] unset($alter); xdebug_debug_zval('obj'); ///obj:(refcount=1, is_ref=0), object(A)[1] 

The meaning of the upper example is that we tied a variable to the created object, and then untied the freshly baked link, made to check whether is_ref=1 or not, as we see it zeroed out, but why it is not clear, we didn’t do anything with the original =)

    2 answers 2

    Only the other day I translated the corresponding chapter .

    In short, new returns a copy, not a link, and if you use the $a = &new Foo() construct, then a reference to it in the global array $globalref will NOT point to the same object as $a .

    In addition, based on the documentation, creating a link is a more resource-intensive process.

    The behavior of xdebug_debug_zval I would explain as follows:

     $obj = &new A; 

    new A creates an instance of the class, passes its copy to the & operator, which generates a reference to it and then passes it to the assignment operator.

    That is, in $obj we keep a link to a copy of the instantiated class A From this it turns out that, in fact, the variable containing this class does not exist. There is only a link to the section of memory where it is located.

    UPD

    That's what happens.

    1. An object is created and a link to it is created - refcount=1 . That is, this &obj is a link. From here is_ref=1 .
    2. The link to the $alter link is created, the reference count is incremented - refcount=2 . It is also a link, hence is_ref=1 .
    3. You delete a link to an object to which there are 2 links. The reference count decreases by one AND (!!!) the last variable that refers to this object automatically ceases to be a reference, since it is the last. If you are familiar with * nix, then this is practically an analogue of hardlink.

    Try this code here.

     $a = 'a'; $b = &$a; $с = &$a; xdebug_debug_zval('a'); xdebug_debug_zval('b'); xdebug_debug_zval('c'); unset($a); xdebug_debug_zval('b'); xdebug_debug_zval('c'); unset($b); xdebug_debug_zval('c'); 

    END UPD

    PHP 7


    Parse error : syntax error, unexpected 'new' (T_NEW) in [...] [...] on line 6

    PHP 5.3-5.6


    Deprecated : Assigning of [...] [...] on line 6

    • And how to explain the conclusions of the function xdebug_debug_zval('obj'); - MaximPro
    • @MaximPro but what exactly is not clear to you in it? - rjhdby
    • I wrote that it is not clear =) - MaximPro
    • @MaximPro updated again. It seems now that thing. - rjhdby
    • Ha, well, damn, this is still funny: "An object is created and a link to it is created - refcount=1 That is, this &obj is a link. Hence, is_ref=1 ". Why is it funny? Well, because I have not seen this once, even the same creation of a variable !. You will never see refcount=1, is_ref=1 when creating a variable. Usually when there is a link in one instance, then is_ref=0 . Have you tried my latest code? is_ref - indicates is this variable a hard link so? Well, how can one link be hard? - MaximPro

    Your php version is lower than 5.3.0 and 5.3.0 will display a warning that this method is deprecated, since in the version above 5.3.0 an object is automatically created by reference, and from version 7 you will receive a parser error.

    • version 5.6, but the question is not about an error - MaximPro
    • I do not see a precedent of the EU honestly in your post: refcount (reference count), contains the number of variable names that point to the given container zval, is_ref and represents a boolean value indicating whether the variable is part of a "reference set" or not. All this works to optimize memory, therefore, by deleting the link, you kind of recursively go up and delete all the links, but the object itself remains, therefore refcount=1 and no links is_ref=0 - Redr01d
    • Perform several tests with variables and the function xdebug_debug_zval('obj'); , then write, if you do not see, then maybe you should not answer or write? PS I spent a bunch of tests and for me the value of refcount=1, is_ref=1 is at least strange! Why I wrote in the question! - MaximPro