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

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

        <legend id='X10jR'><style id='X10jR'><dir id='X10jR'><q id='X10jR'></q></dir></style></legend>

        为什么在 setter 方法中返回 $this ?

        时间:2023-10-02
        <i id='ZAi5i'><tr id='ZAi5i'><dt id='ZAi5i'><q id='ZAi5i'><span id='ZAi5i'><b id='ZAi5i'><form id='ZAi5i'><ins id='ZAi5i'></ins><ul id='ZAi5i'></ul><sub id='ZAi5i'></sub></form><legend id='ZAi5i'></legend><bdo id='ZAi5i'><pre id='ZAi5i'><center id='ZAi5i'></center></pre></bdo></b><th id='ZAi5i'></th></span></q></dt></tr></i><div id='ZAi5i'><tfoot id='ZAi5i'></tfoot><dl id='ZAi5i'><fieldset id='ZAi5i'></fieldset></dl></div>
          <bdo id='ZAi5i'></bdo><ul id='ZAi5i'></ul>
              <tbody id='ZAi5i'></tbody>

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

              • <legend id='ZAi5i'><style id='ZAi5i'><dir id='ZAi5i'><q id='ZAi5i'></q></dir></style></legend>
                <tfoot id='ZAi5i'></tfoot>

                  本文介绍了为什么在 setter 方法中返回 $this ?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  检查 Zend 框架,我发现所有的 setter 方法(我检查过的)都返回它所在类的实例.它不仅设置一个值,还返回 $this.例如:

                  Examining Zend Framework, I found that all setter methods (of those I’ve examined) return the instance of the class it lives in. It doesn't only set a value but also returns $this. For example:

                    /*   Zend_Controller_Router   */
                  public function setGlobalParam($name, $value) {
                      $this->_globalParams[$name] = $value;
                      return $this;
                  }
                  
                    /*    Zend_Controller_Request    */
                  public function setBaseUrl($baseUrl = null) {
                      // ... some code here ...
                      $this->_baseUrl = rtrim($baseUrl, '/');
                      return $this;
                  }
                  
                    /*    Zend_Controller_Action    */
                  public function setFrontController(Zend_Controller_Front $front) {
                      $this->_frontController = $front;
                      return $this;
                  }
                  

                  等等.每个公共 setter 都返回 $this.而且它不仅适用于 setter,还有其他返回 $this 的操作方法:

                  And so on. Every public setter returns $this. And it's not only for setters, there are also other action methods that return $this:

                  public function addConfig(Zend_Config $config, $section = null) {
                      // ... some code here ...
                      return $this;
                  }
                  

                  为什么需要这个?返回 $this 有什么作用?有什么特殊含义吗?

                  Why is this needed? What does returning $this do? Does it have some special meaning?

                  推荐答案

                  return $this 允许链接如下方法:

                  $foo->bar('something')->baz()->myproperty
                  

                  这篇关于为什么在 setter 方法中返回 $this ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:php zend gdata - 使用 oauth 获取谷歌文档列表 下一篇:.zfproject.xml 在 Zend Framework 项目中是必须的吗?

                  相关文章

                  最新文章

                  <legend id='xGkAp'><style id='xGkAp'><dir id='xGkAp'><q id='xGkAp'></q></dir></style></legend>
                    • <bdo id='xGkAp'></bdo><ul id='xGkAp'></ul>

                    1. <tfoot id='xGkAp'></tfoot>
                    2. <small id='xGkAp'></small><noframes id='xGkAp'>

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