Widziałem kilka projektów, w których klasy mają uzyskać i ustawić metody manipulowania danymi wstawiania. Pozwól mi mieć przykład tutaj:Czy warto tworzyć i ustawiać metody w 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);
}
}
Powyżej znajduje się przykładowa klasa, którą widziałem. Poniżej znajduje się opis sposobu ich używania:
$student = new Student;
$student->setTableID = 1;
$student->setFullName('My Name');
$student->setGender('Male');
$student->setAddress('this is my address');
$studen->UpdateStudent();
Czy warto to robić w ten sposób? Osobiście uważam, że nie ma sensu ustawiać pola, a następnie pobierać i aktualizować w nim zapisy. Zrobienie tego w każdym module zajmuje naprawdę dużo czasu. Jaki jest najlepszy sposób na radzenie sobie z takimi rzeczami? Czy istnieją jakieś zabezpieczenia w ten sposób?
Modyfikatory dostępu (takie jak "prywatne") są często źle interpretowane jako funkcja bezpieczeństwa. W obecnym inkarnowaniu w C++ zamierzali ograniczyć ekspozycję ABI (nie API); co nie jest tak istotne dla PHP i języków skryptowych. Wymaganie ustawiających i pobierających jest często efektem ubocznym, ale nie jest obiektowe. Zobacz także [PHP Getters and setters: evil or necessary evil?] (Http://berryllium.nl/2011/02/getters-and-setters-evil-or-necessary-evil/) oraz [Java: Getters and setters are evil] (http://www.javaworld.com/javaworld/jw-09-2003/jw-0905-toolbox.html) – mario
możliwy duplikat [Czy to naprawdę źle, nie używając ustawiaczy i pobierających?] (http: // /stackoverflow.com/questions/808348/is-it-really-that-wrong-not-using-setters-and-getters) – mario