Because the expression "$ a-b-c-d-e-f-g-h-i-j = & $ null;" returns several objects inside the other, since it does not even exist?

4

I was doing some benchmarking tests with PHP references and I came across a curious example.

$a->b->c->d->e->f->g->h->i->j =& $null;

Both variables $a and Null do not exist in any scope of my script.

When I give print_r($a) , see what is returned:

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

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

link

The strange thing is that when I try to do this without reference, look what happens:

$a->b->c = 1;
  

PHP Warning: Creating default object from empty value on line 1

Why is this happening in PHP? What is the reason for this behavior (the object being created out of nowhere and not having issued any Warning)?

Why did the $a and $null variables not return Undefined variable when I used references?

    
asked by anonymous 11.05.2016 / 21:21

1 answer

2

First you have different things:

  • In the first case you are assigning a reference to an object, in short that is what it is, a reference, no matter what you have it, it just "bets."

  • The second case is a direct assignment, you want to reserve a location in memory to assign a value.

Error

The error caused is a warning.

  

PHP Warning: Creating default object from empty value on line 1

Whether you want to assign the value to an " attribute " (object), neither object has yet to do so, it needed to create a defaulf object.

Addendum

Note that if you do:

$b->c = 6;

$a->b =& $b;

var_dump($a->b);

warning is fired at $b , and in $a->b it only resumed the reference it saved.

And why using referrals gave no error?

In order to understand the exemplified behavior, we first have to calmly analyze what is written in the PHP Manual, referring to Assignment by reference .

  

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

If you assign, pass, or return an undefined variable by reference, it will be created, and set for NULL

That is, if you use the assignment signal by reference ( =& ) for a variable that does not exist, it will be created.

To analyze, take the test:

$a =& $b;

$c =& $d;

var_dump(get_defined_vars());

The result will be:

['a' => NULL, 'b' => NULL, 'c' => NULL, 'd' => NULL]

Another proof that the above statement is correct can be seen for those who have already used the functions preg_match or parse_str . They use references to bring the result of the function (instead of return ), even if the variable does not exist, it happens.

Example:

 preg_match('/eu_amo_regex/', 'eu_amo_regex_sim', $vou_passar_a_existir);

 print_r($vou_passar_a_existir);// Essa foi criada na passagem do argumento

See in the function's skeleton the reference in the 3 parameter:

int preg_match ( string $pattern , string $subject [, array &$matches [, int  $flags = 0 [, int $offset = 0 ]]] )

In your case, the j attribute of the object will be NULL , and the $null variable too. The entire structure of the object is created because you are using reference, so it does not give error.

Related question: How do C pointers work?

    
13.04.2017 / 14:59