我见过一些项目,其中的类具有 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:
它有一个优点:
如果这个优势很有意义(例如您正在编写一个可重用的软件库),那么编写属性而不是裸字段就非常有意义.否则,您的工作就是没有任何好处.
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 方法吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!