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

      <tfoot id='ycnkt'></tfoot>

      • <bdo id='ycnkt'></bdo><ul id='ycnkt'></ul>

    1. <legend id='ycnkt'><style id='ycnkt'><dir id='ycnkt'><q id='ycnkt'></q></dir></style></legend>

        值得在 OOP 中创建 get 和 set 方法吗?

        Is it worth making get and set methods in OOP?(值得在 OOP 中创建 get 和 set 方法吗?)

          <tbody id='1EXCA'></tbody>

            <bdo id='1EXCA'></bdo><ul id='1EXCA'></ul>

            <small id='1EXCA'></small><noframes id='1EXCA'>

                • <legend id='1EXCA'><style id='1EXCA'><dir id='1EXCA'><q id='1EXCA'></q></dir></style></legend>

                • <tfoot id='1EXCA'></tfoot>
                • <i id='1EXCA'><tr id='1EXCA'><dt id='1EXCA'><q id='1EXCA'><span id='1EXCA'><b id='1EXCA'><form id='1EXCA'><ins id='1EXCA'></ins><ul id='1EXCA'></ul><sub id='1EXCA'></sub></form><legend id='1EXCA'></legend><bdo id='1EXCA'><pre id='1EXCA'><center id='1EXCA'></center></pre></bdo></b><th id='1EXCA'></th></span></q></dt></tr></i><div id='1EXCA'><tfoot id='1EXCA'></tfoot><dl id='1EXCA'><fieldset id='1EXCA'></fieldset></dl></div>
                  本文介绍了值得在 OOP 中创建 get 和 set 方法吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我见过一些项目,其中的类具有 get 和 set 方法来操作插入数据.让我在这里举个例子:

                  I have seen some some projects in which classes are having get and set methods to manipulate insert data. Let me have an example here :

                      class Student extends dbClass
                  {
                      private $TableID;
                      private $FullName;
                      private $Gender;
                      private $Address;
                  
                  
                  
                  
                      function setTableID($Value)
                      {
                          $this->TableID = $Value;
                      }
                  
                      function getTableID()
                      {
                          return $this->TableID;
                      }
                  
                      function setFullName($Value)
                      {
                          $this->FullName = $Value;
                      }
                  
                      function getFullName()
                      {
                          return $this->FullName;
                      }
                  
                      function setGender($Value)
                      {
                          $this->Gender = $Value;
                      }
                  
                      function getGender()
                      {
                          return $this->Gender;
                      }
                  
                      function setAddress($Value)
                      {
                          $this->Address = $Value;
                      }
                  
                      function getAddress()
                      {
                          return $this->Address;
                      }
                  
                  
                      function UpdateStudent()
                      {
                          $sql = "UPDATE INTO usertable SET
                          FullName = '".$this->getFullName()."',
                          Gender = '".$this->getGender()."',
                          Address = '".$this->getAddress()."'
                          where TableID='".$this->getTableID()."'";
                          $this->query($sql);
                      }
                  }
                  

                  以上是我见过的示例类.以下是他们如何使用它的过程:

                  Above is the example class that i have seen. And below is the process how they are using it :

                  $student = new Student;
                  $student->setTableID = 1;
                  $student->setFullName('My Name');
                  $student->setGender('Male');
                  $student->setAddress('this is my address');
                  
                  $studen->UpdateStudent();
                  

                  这样做值得吗?我个人认为设置字段然后获取和更新其中的记录是没有用的.为每个模块制作它真的需要很多时间.处理此类事情的最佳方法是什么?这样做是否有任何安全问题?

                  Is it worth doing this way? I personally think its useless to set field and then get and update records in it. It really takes a lot of time to make it for every module. What is the best way to handle such thing? Is there any security concerned doing it in this way?

                  推荐答案

                  这样做值得吗?

                  这取决于.

                  通过暴露智能"属性(即 getter 和/或 setter)从用户中提取字段有两个缺点:

                  Abstracting a field from the user by exposing a "smart" property (i.e. getter and/or setter) has two disadvantages:

                  1. 你需要写更多的代码;如果该属性没有真正做任何聪明的事情,那么这段代码就没有任何用处.
                  2. 该属性的用户有点不方便,因为他们还需要输入更多内容.

                  它有一个优点:

                  1. 将来您可以向属性添加逻辑,即使之前没有逻辑,而不会破坏用户的代码.
                  1. In the future you can add logic to the properties even if there was none before without breaking your users' code.

                  如果这个优势很有意义(例如您正在编写一个可重用的软件库),那么编写属性而不是裸字段就非常有意义.否则,您的工作就是没有任何好处.

                  If this advantage is meaningful (e.g. you are writing a reusable software library) then it makes great sense to write properties instead of bare fields. If not, you are doing work for no benefit.

                  处理此类事情的最佳方法是什么?

                  What is the best way to handle such thing?

                  您可以覆盖神奇的 __get__set 函数(可能在基类中,因此您也可以继承覆盖)以自动将属性访问转发给您的 getter 和二传手.简化代码:

                  You can override the magic __get and __set functions (perhaps in a base class so you can inherit the override as well) to automatically forward property accesses to your getters and setters. Simplified code:

                  public function __get($name) {
                      $getter = 'get'.$name;
                      if (method_exists($this, $getter)) {
                          return $this->$getter();
                      }
                  
                      $message = sprintf('Class "%1$s" does not have a property named "%2$s" or a method named "%3$s".', get_class($this), $name, $getter);
                      throw new OutOfRangeException($message);
                  }
                  
                  public function __set($name, $value) {
                      $setter = 'set'.$name;
                      if (method_exists($this, $setter)) {
                          return $this->$setter($value);
                      }
                  
                      $getter = 'get'.$name;
                      if (method_exists($this, $getter)) {
                          $message = sprintf('Implicit property "%2$s" of class "%1$s" cannot be set because it is read-only.', get_class($this), $name);
                      }
                      else {
                          $message = sprintf('Class "%1$s" does not have a property named "%2$s" or a method named "%3$s".', get_class($this), $name, $setter);
                      }
                      throw new OutOfRangeException($message);
                  }
                  

                  Caveat emptor: 由于 __get__set 被覆盖,__isset__unset> 也应该被覆盖!

                  Caveat emptor: Since __get and __set are overridden, __isset and __unset should be overridden as well!

                  这样做是否有任何安全问题?

                  Is there any security concerned doing it in this way?

                  不,根本没有(假设您没有意外插入错误).

                  No, none at all (assuming you don't insert bugs accidentally).

                  这篇关于值得在 OOP 中创建 get 和 set 方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Appending GET parameters to URL from lt;formgt; action(将 GET 参数附加到来自 lt;formgt; 的 URL行动)
                  Forcing quot;Save Asquot; dialog via jQuery GET(强制“另存为通过 jQuery GET 对话框)
                  PHP - get certain word from string(PHP - 从字符串中获取某个单词)
                  How to debug a get request in php using curl(如何使用 curl 在 php 中调试 get 请求)
                  get a # from a url in php(从 php 中的 url 获取 #)
                  PHP - include() file not working when variables are put in url?(PHP - 将变量放入 url 时,include() 文件不起作用?)
                    <bdo id='JjHIa'></bdo><ul id='JjHIa'></ul>

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

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

                        <legend id='JjHIa'><style id='JjHIa'><dir id='JjHIa'><q id='JjHIa'></q></dir></style></legend>
                            <tbody id='JjHIa'></tbody>