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

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

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

      1. 是否值得在 OOP 中使用 get 和 set 方法?

        时间:2024-08-23
        <legend id='XwERZ'><style id='XwERZ'><dir id='XwERZ'><q id='XwERZ'></q></dir></style></legend>
            <tbody id='XwERZ'></tbody>
          <tfoot id='XwERZ'></tfoot>
          <i id='XwERZ'><tr id='XwERZ'><dt id='XwERZ'><q id='XwERZ'><span id='XwERZ'><b id='XwERZ'><form id='XwERZ'><ins id='XwERZ'></ins><ul id='XwERZ'></ul><sub id='XwERZ'></sub></form><legend id='XwERZ'></legend><bdo id='XwERZ'><pre id='XwERZ'><center id='XwERZ'></center></pre></bdo></b><th id='XwERZ'></th></span></q></dt></tr></i><div id='XwERZ'><tfoot id='XwERZ'></tfoot><dl id='XwERZ'><fieldset id='XwERZ'></fieldset></dl></div>

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

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

                • 本文介绍了是否值得在 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);
                  }
                  

                  警告:由于 __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 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何最快地计算 php 中设置的位数? 下一篇:在 PHP 中显示来自 Active Directory 的缩略图照片

                  相关文章

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

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

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