How does the undeclared variable reference work in PHP?

4

In PHP, according to the manual, the reference of one variable is for one variable pointing to the same place where the other is in memory. I also saw that when we declare we refer to a variable that does not exist, it is created and set to NULL .

Then came a question.

We have the following codes.

In this allocation attempt below, a runtime error will be generated.

$a  = $b->b->c->d->e->f->g; 

/*
    Undefined variable 'a'
    Trying to get property of non-object (6 vezes)
*/


var_dump($a, $b); // resultado: NULL NULL

Now, when we use the assignment operator by reference ( & ), no error is generated, and the object (which does not exist) is magically created.

$a  =& $b->b->c->d->e->f->g; 
var_dump($a);
/*
    a = NULL
*/


print_r($b);

/* 

stdClass Object
(
    [b] => stdClass Object
        (
            [c] => stdClass Object
                (
                    [d] => stdClass Object
                        (
                            [e] => stdClass Object
                                (
                                    [f] => stdClass Object
                                        (
                                            [g] => 
                                        )

                                )

                        )

                )

        )

)

*/
  • I would like to know how this works internally, in the interpreter of PHP .
  • It is recommended to create an object within the other in this way (as I have seen it being used in an implementation of Dot Notation for PHP, but instead of an object, an array is used)?
  • asked by anonymous 30.01.2015 / 12:00

    1 answer

    4
      

    If you assign, pass, or return an undefined variable by   reference, it will be created.

    link

      

    A PHP variable is stored in a container called "zval". a   container zval contains, in addition to the type and value of the variable, two bits   additional information. The first is called "is_ref" and is a   Boolean value that indicates whether or not the variable is part of a "set   With this bit, the PHP engine knows how to differentiate   between normal variables and references.

         

    Since PHP started allowing user-level referrals,   created by the & operator, the zval container now has a mechanism   of internal reference count to optimize memory usage.   This second piece of additional information, called "refcount", contains   how many variable names (also called symbols) point to   this container zval.

         

    All symbols are stored in a symbol table, of which   there is one by scope. There is a space for the main script (that is, a   required by the browser), as well as one for each function or method.

    link

    So when a variable is copied $a = $b , PHP internally uses the reference until the $a copy is modified.

    So you should avoid creating variables through unnecessary references, since they will not save your state in memory and increase the chance of logical errors that are hard to figure out.

        
    30.01.2015 / 14:10