He visto algunos proyectos en los que las clases tienen métodos get y set para manipular datos de inserción. Permítanme tener un ejemplo aquí:¿Vale la pena hacer los métodos get y set en OOP?
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 es la clase de ejemplo que he visto. Y debajo está el proceso de cómo lo están usando:
$student = new Student;
$student->setTableID = 1;
$student->setFullName('My Name');
$student->setGender('Male');
$student->setAddress('this is my address');
$studen->UpdateStudent();
¿Vale la pena hacerlo de esta manera? Personalmente creo que es inútil establecer un campo y luego obtener y actualizar registros en él. Realmente lleva mucho tiempo hacerlo para cada módulo. ¿Cuál es la mejor manera de manejar tal cosa? ¿Hay algún problema de seguridad al hacerlo de esta manera?
Los modificadores de acceso (como 'privado') a menudo se malinterpretan como característica de seguridad. En su encarnación actual de C++, en realidad tenían la intención de limitar la exposición ABI (no API); que realmente no es tan relevante para PHP y los lenguajes de scripting. Exigir setters y getters suele ser un efecto secundario, pero no está muy orientado a objetos. Ver también [Getters y setters de PHP: malvado o mal necesario?] (Http://berryllium.nl/2011/02/getters-and-setters-evil-or-necessary-evil/) y [Java: Getters y setters son evil] (http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html) – mario
posible duplicado de [¿Es realmente así de malo no usar setters y getters?] (http://stackoverflow.com/questions/808348/is-it-really-that-wrong-not-using-setters-and-getters) – mario