Why does the expression "$a->b->c->d->e->f->g->h->i->j =& $null;" return several objects within each other, and it doesn’t even exist?

Asked

Viewed 220 times

4

I was doing some tests with value assignments by PHP references and came across a curious example.

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

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

When I give a 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] => 
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

https://ideone.com/zvaLkJ

The weird thing is, 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 does this happen in PHP? What is the reason for this behavior (the object being created out of thin air and not issuing any Warning)?

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

  • Just missing the image of Ryu giving a hadouken in the result of array

  • @Brunocosta worse than it is, there is an explanation. I’m just waiting for someone who explains better than what I already know

  • You guys, nobody’s taking a chance on answering that?

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 has there, it just "bet".

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

Error

The error caused is a "Warning" (warning/warning).

PHP Warning: Creating default Object from Empty value on line 1

Want to say that you want to assign the value in a "attribute"(object), being that not even object has yet, for that he needed to create an object defaulf.

Addendum

Note that if you do :

$b->c = 6;

$a->b =& $b;

var_dump($a->b);

The Warning is shot at the $b, and in the $a->b he just resumed the reference stored in it.

And why using references was not wrong?

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

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

And I add: If you assign, pass, or return an undefined variable by reference, it will be created, and arrow for NULL

That is, if you use the attribution 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 to those who have already used the functions preg_match or parse_str. They use references to bring the function result (instead of return), even if the variable does not exist, it happens to exist.

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 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 attribute j of the object will be NULL, and the variable $null also. The whole object structure is created because you are using reference, so it does not give error.

Related question : How the C pointers work?

  • kkk, I was just about to reply. I can add important information to the answer?

  • @Wallacemaxters at will, the content here is pub :D.

  • @Wallacemaxters as you added 2/3 of the reply I left as wiki :D

Browser other questions tagged

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