是否值得在 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:
- 您需要编写更多代码;如果该属性并没有真正做任何聪明的事情,那么这就是没有任何用处的代码.
- 该属性的用户有点不便,因为他们还必须输入更多内容.
而且它有一个优点:
- 将来您可以向属性添加逻辑,即使之前没有逻辑,而不会破坏用户的代码.
- 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).
相关文章