• <small id='yM9xD'></small><noframes id='yM9xD'>

    <i id='yM9xD'><tr id='yM9xD'><dt id='yM9xD'><q id='yM9xD'><span id='yM9xD'><b id='yM9xD'><form id='yM9xD'><ins id='yM9xD'></ins><ul id='yM9xD'></ul><sub id='yM9xD'></sub></form><legend id='yM9xD'></legend><bdo id='yM9xD'><pre id='yM9xD'><center id='yM9xD'></center></pre></bdo></b><th id='yM9xD'></th></span></q></dt></tr></i><div id='yM9xD'><tfoot id='yM9xD'></tfoot><dl id='yM9xD'><fieldset id='yM9xD'></fieldset></dl></div>
    <legend id='yM9xD'><style id='yM9xD'><dir id='yM9xD'><q id='yM9xD'></q></dir></style></legend>

  • <tfoot id='yM9xD'></tfoot>

        <bdo id='yM9xD'></bdo><ul id='yM9xD'></ul>
      1. 在 PHP 中按引用返回

        时间:2023-10-12

        <small id='JYnxh'></small><noframes id='JYnxh'>

          <tfoot id='JYnxh'></tfoot>

          • <bdo id='JYnxh'></bdo><ul id='JYnxh'></ul>
            <i id='JYnxh'><tr id='JYnxh'><dt id='JYnxh'><q id='JYnxh'><span id='JYnxh'><b id='JYnxh'><form id='JYnxh'><ins id='JYnxh'></ins><ul id='JYnxh'></ul><sub id='JYnxh'></sub></form><legend id='JYnxh'></legend><bdo id='JYnxh'><pre id='JYnxh'><center id='JYnxh'></center></pre></bdo></b><th id='JYnxh'></th></span></q></dt></tr></i><div id='JYnxh'><tfoot id='JYnxh'></tfoot><dl id='JYnxh'><fieldset id='JYnxh'></fieldset></dl></div>

            <legend id='JYnxh'><style id='JYnxh'><dir id='JYnxh'><q id='JYnxh'></q></dir></style></legend>
              <tbody id='JYnxh'></tbody>
                1. 本文介绍了在 PHP 中按引用返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我尝试了谷歌搜索,尝试了 PHP 文档,在 Stack Overflow 上搜索了答案,但没有找到任何令人满意的答案.我正在读一本书,其中作者使用了引用返回但从未解释过它是什么.作者使用的代码是

                  I tried Googling, tried PHP Documentation, searched Stack Overflow for an answer but couldn't find anything satisfactory. I was reading a book in which author have made use of Return by Reference but never explained what it is. The code used by the author is

                  function &getSchool() {
                      return $this->school;
                  }
                  

                  谁能用简单的词来解释一下这个概念.

                  Can someone explain in simple words with an example about this concept.

                  推荐答案

                  假设你有这个类:

                  class Fruit {
                      private $color = "red";
                  
                      public function getColor() {
                          return $this->color;
                      }
                  
                      public function &getColorByRef() {
                          return $this->color;
                      }
                  } 
                  

                  该类有一个私有属性和两个可让您访问它的方法.一个按值返回(默认行为),另一个按引用返回.两者的区别在于:

                  The class has a private property and two methods that let you access it. One returns by value (default behavior) and the other by reference. The difference between the two is that:

                  • 使用第一种方法时,您可以对返回值进行更改,这些更改不会反映在 Fruit 的私有属性中,因为您实际上是在修改属性值的副本.
                  • 当使用第二种方法时,您实际上是在取回Fruit::$color的别名——一个不同的名称你指的是相同的变量.因此,如果您对其进行任何操作(包括修改其内容),您实际上是在直接对属性的值执行相同的操作.
                  • When using the first method, you can make changes to the returned value and those changes will not be reflected inside the private property of Fruit because you are actually modifying a copy of the property's value.
                  • When using the second method, you are in fact getting back an alias for Fruit::$color -- a different name by which you refer to the same variable. So if you do anything with it (including modifying its contents) you are in fact directly performing the same action on the value of the property.

                  这是一些测试它的代码:

                  Here's some code to test it:

                  echo "
                  TEST RUN 1:
                  
                  ";
                  $fruit = new Fruit;
                  $color = $fruit->getColor();
                  echo "Fruit's color is $color
                  "; 
                  $color = "green"; // does nothing, but bear with me
                  $color = $fruit->getColor();
                  echo "Fruit's color is $color
                  "; 
                  
                  echo "
                  TEST RUN 2:
                  
                  ";
                  $fruit = new Fruit;
                  $color = &$fruit->getColorByRef(); // also need to put & here
                  echo "Fruit's color is $color
                  "; 
                  $color = "green"; // now this changes the actual property of $fruit
                  $color = $fruit->getColor();
                  echo "Fruit's color is $color
                  "; 
                  

                  查看实际操作.

                  警告:我觉得有必要提一下,虽然引用确实有合法用途,但它们是那些应该很少使用的功能之一,并且只有在您仔细考虑了任何替代方案的情况下才能使用先.经验不足的程序员倾向于过度使用引用,因为他们认为引用可以帮助他们解决特定问题,同时也没有看到使用引用的缺点(作为一项高级功能,其细微差别远非明显).

                  Warning: I feel obliged to mention that references, while they do have legitimate uses, are one of those features that should be used only rarely and only if you have carefully considered any alternatives first. Less experienced programmers tend to overuse references because they see that they can help them solve a particular problem without at the same time seeing the disadvantages of using references (as an advanced feature, its nuances are far from obvious).

                  这篇关于在 PHP 中按引用返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:PHP 变量引用和内存使用 下一篇:jQuery 自动完成 Mysql PHP

                  相关文章

                    <bdo id='nH1Ze'></bdo><ul id='nH1Ze'></ul>

                  1. <legend id='nH1Ze'><style id='nH1Ze'><dir id='nH1Ze'><q id='nH1Ze'></q></dir></style></legend>
                    <i id='nH1Ze'><tr id='nH1Ze'><dt id='nH1Ze'><q id='nH1Ze'><span id='nH1Ze'><b id='nH1Ze'><form id='nH1Ze'><ins id='nH1Ze'></ins><ul id='nH1Ze'></ul><sub id='nH1Ze'></sub></form><legend id='nH1Ze'></legend><bdo id='nH1Ze'><pre id='nH1Ze'><center id='nH1Ze'></center></pre></bdo></b><th id='nH1Ze'></th></span></q></dt></tr></i><div id='nH1Ze'><tfoot id='nH1Ze'></tfoot><dl id='nH1Ze'><fieldset id='nH1Ze'></fieldset></dl></div>

                  2. <small id='nH1Ze'></small><noframes id='nH1Ze'>

                    <tfoot id='nH1Ze'></tfoot>