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

Is it worth making get and set methods in OOP?(是否值得在 OOP 中使用 get 和 set 方法?)
本文介绍了是否值得在 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 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)