是否值得在OOP中获取和设置方法? [英] Is it worth making get and set methods in OOP?

查看:79
本文介绍了是否值得在OOP中获取和设置方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到了一些项目,其中的类有get和set方法来操作插入数据。让我举个例子:

  class Student扩展dbClass 
{
private $ TableID;
private $ FullName;
私人$性别;
私人$地址;




函数setTableID($ Value)
{
$ this-> TableID = $ Value;
}

函数getTableID()
{
return $ this-> TableID;
}

函数setFullName($ Value)
{
$ this-> FullName = $ Value;
}

函数getFullName()
{
return $ this-> FullName;
}

函数setGender($ Value)
{
$ this-> Gender = $ Value;
}

函数getGender()
{
return $ this-> Gender;
}

函数setAddress($ Value)
{
$ this-> Address = $ Value;
}

函数getAddress()
{
return $ this->地址;


$ b函数UpdateStudent()
{
$ sql =UPDATE INTO usertable SET
FullName ='。$ this- > getFullName()。',
Gender ='。$ this-> getGender()。',
Address ='。$ this-> getAddress()。'
where TableID ='。$ this-> getTableID()。';
$ this-> query($ sql);




$ b $ p
$ b

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

  $ student = new Student; 
$ student-> setTableID = 1;
$ student-> setFullName('My Name');
$ student-> setGender('Male');
$ student-> setAddress('这是我的地址');

$ studen-> UpdateStudent();

值得这样做吗?我个人认为它无用设置字段,然后获取并更新记录。每个模块都需要花费很多时间。处理这种事情的最佳方式是什么?
是否有任何担保以此方式进行?

解决方案


是否值得这样做?




这取决于。

通过暴露智能属性(即getter和/或setter) )有两个缺点:$ b​​
$ b


  1. 你需要编写更多的代码;如果这个属性并没有真正做出任何聪明的事情,那么这个代码不会有用。

  2. 该属性的用户稍微感到不便,因为他们必须输入更多。

它有一个优点:$ b​​
$ b


  1. 将来您可以将逻辑添加到属性中,即使没有破坏用户代码 之前没有

如果这个优点是有意义的(例如,你正在编写一个可重用的软件库),那么编写属性而不是裸露的字段是很有意义的。如果没有,你正在做的工作没有任何好处。


处理这种事情的最佳方式是什么?


您可以覆盖魔术 __ get __ set 函数(可能在基类中,以便您可以继承重写)自动将属性访问转发给您的getter和setter。简化代码:

pre $ public function __get($ name){
$ getter ='get'。$ name;
if(method_exists($ this,$ getter)){
return $ this-> $ getter();


$ message = sprintf('Class'%1 $ s没有名为%2 $ s的属性或名为%3 $ s的方法。 ,get_class($ this),$ name,$ getter);
抛出new \OutOfRangeException($ message);
}

公共函数__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('class'%1 $ s'的隐式属性%2 $ s不能设置,因为它是只读的',get_class($ this),$ name);
}
else {
$ message = sprintf('Class'%1 $ s没有名为%2 $ s的属性或名为%3 $ s的方法) ',get_class($ this),$ name,$ setter);
}
抛出new \OutOfRangeException($ message);

注意: __ get __ set 被覆盖, __ isset __ unset 也应该被覆盖!


有没有关于这样做的安全性?


不,完全没有(假设您不会意外插入错误)。


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?

解决方案

Is it worth doing this way?

It depends.

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

  1. You need to write more code; if the property doesn't really do anything smart, this is code that does nothing useful.
  2. The user of the property is slightly inconvenienced because they have to type a little more as well.

And it has one advantage:

  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?

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: 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中获取和设置方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆