duanjiao5723 2017-09-28 06:11
浏览 43
已采纳

引用不是php中的指针?

I am new to php and learning it from php.net. Can anyone tell in the summary why does that page (http://php.net/manual/en/language.references.arent.php) says What references are not & References are not pointers.

I am beginner so please can anyone explain me in simple and easy words ?

  • 写回答

2条回答 默认 最新

  • duanji2772 2017-09-28 06:23
    关注

    In simple words, references are aliases.

    A variable in PHP is stored in two pieces: the name and the value. The name points to the value.

    $x = 2;
    $y = $x;
    $z = &$x;
    

    When $x = 2; is executed, the name x is stored in the symbol table of the current scope and the value 2 is stored in a zval (don't ask, it's the internal name of a data structure that stores a value in PHP).

    When $y = $x; is executed, the name y is stored in the symbol table of the current scope and the value of $x (2) is copied into a new zval structure.

    When $z = &$x; is executed, the name z is stored in the symbol table of the current scope but a new zval is not created. Instead, z is set to point to the same zval as x.

    The memory used by the variables $x, $y and $z looks like this:

    +---------+                +---------+
    |    x    | -------------> |    2    |
    +---------+                +---------+
                                    ^
    +---------+                     |      +---------+
    |    y    | -------------------------> |    2    |
    +---------+                     |      +---------+
                                    |
    +---------+                     |
    |    z    | --------------------+
    +---------+
    

    When a value is passed by reference to a function or a function returns a reference the same things happen, only the names are stored in different symbol tables (remark the "current scope" in the explanation above).

    Let' see this code:

    function f(& $z) {
        $y = $z;
        $z = $z + 2;
    }
    $x = 2;
    f($x);
    

    After $x = 2; the memory looks like this:

    +---------+                +---------+
    |    x    | -------------> |    2    |
    +---------+                +---------+
    

    During the execution of function f(), the memory looks like this:

    +===== global ====+
    |   +---------+   |            +---------+
    |   |    x    | -------------> |    4    |
    |   +---------+   |            +---------+
    +=================+                 ^
                                        |
    +====== f() ======+                 |
    |   +---------+   |                 |      +---------+
    |   |    y    | -------------------------> |    2    |
    |   +---------+   |                 |      +---------+
    |                 |                 |
    |   +---------+   |                 |
    |   |    z    | --------------------+
    |   +---------+   |
    +=================+
    

    y and z are stored in a different symbol table than x and they are removed (together with the entire symbol table that contains them) when the call to f() returns.

    When y is removed, its value is also removed because there is no name that points to it any more. But, because the value pointed by z is also pointed by x ($z is an alias), the value is not removed together with z and it survives the function call. f() modifies the value using $z; and this change is visible in the main program through the variable $x.

    The things happen in a similar way when a function returns a reference. The function returns a value that is not copied but a new name that points to it is created into the symbols table of the calling code.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效