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?
It depends.
Abstracting a field from the user by exposing a "smart" property (i.e. getter and/or setter) has two disadvantages:
And it has one advantage:
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.
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:Caveat emptor: Since
__get
and__set
are overridden,__isset
and__unset
should be overridden as well!No, none at all (assuming you don't insert bugs accidentally).
Making setters and getters helps enforce OOP encapsulation. Im not sure for PHP, but for many other languages (Java, C++), a good IDE (eclipse/netbeans) will auto-generate these setters and getters for you.
It may not be immediately obvious for simple types, but if any sort of more complex processing has to be performed, then it becomes more obvious.
An example as to why to sometimes use getters and setters: I have code that sets a value in 200 different files:
Imagine the customer suddenly has a new requirement: "all cat names must be prepended by 'Sweet'
Now we must find all declarations of cats and replace their assignments:
On the other hand, If we used a setter method all we need to do is add the code to add 'Sweet ' in one place:
In languages that do not have properties (public member "variables" which actually lead to function calls) using getter/setters instead of public variables is usually recommended. Otherwise you cannot add logic (e.g. when setting a variable) later if people are already using your plain field.
Since PHP is such a language (unfortunately) the answer is yes, use them.