How does the reference to undeclared variables work in PHP?

Asked

Viewed 216 times

4

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

Hence arose a question.

We have the following codes.

In this assignment attempt below, an execution 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 allocation 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] => 
                                        )

                                )

                        )

                )

        )

)

*/
  1. I’d like to know how this works internally, in the interpreter of PHP.
  2. It is recommended to create an object inside the other one this way (as I’ve seen it being used in an Dot Notation for PHP, but instead of an object, an array is used)?

1 answer

4


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

http://php.net/manual/en/language.references.whatdo.php

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

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

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

http://php.net/manual/en/features.gc.refcounting-basics.php

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

Therefore, you should avoid creating variables through needless references, since it will not keep your state in memory and increases the chance of logic errors difficult to discover.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.