Use of getter-setter within class
Should one use under any circumstance开发者_运维技巧s getters-setters of a class within the class?
Getters setters are generally used from outside class from inside directly access the fields. The main advantage/purpose is encapsulation of getter setters,
If your getters setters has some logical code then use it.
for example :
public void setValue(int val){
if(val > 100)
this.val = 0;
else
this.val = val;
}
Also see
- why-use-getters-and-setters
- More on getters and setters
Yes, getters and setters are useful. Because PHP doesn't support type hinting for simple types like int or string, you cannot force a value to be of the right type.
By using a setter, you always have the possibility to check the value that is set. When a value set to an int property isn't an int, you can choose to typecast it, or raise an error, instead of just accepting the wrong value.
That will make debugging and maintaining your application a lot easier. So it's a good idea to use getters and setters, even if they do not contain much logic other than these checks.
@GolezTrol
There is no PHP badge on topic and you are wrong. What you are describing has nothing to do with setters. You can force type on parameter (in PHP) by using any method not only a setter.
You can write:
setX( X $x ){ ...}
setY( Y $y ){ ...}
or just:
iAmMethodNotASetter( X $x, Y $y){
//20lines of code here end then:
$this->x = $x;
$this->y = $y;
}
Like you see I didn't need setters to enforce type in object properties.
And throwing error in setter after checking variable type is bad idea anyway. It is common error of programmers who transitioned from the language statically typed to dynamically type language.
Setters and geters are CONVENTION and they don't enforce anything! Today we usually use them for creation of Plain Old Java Objects. (POJO - in php word POPO) So it is just a convetion (standard) for creation of object that can by use between libraries or projects.
You can combine setters with type checking or whatever but it dosn't make them somthing more than they are.
About Encapsulation:
@org.life.java - Jigar Joshi
"The main advantage/purpose is encapsulation of getter setters, "
@Peter Alexander
"You're supposed to use the getters and setter almost everywhere, including inside "the class. If you don't, then you are potentially breaking encapsulation" "Getters and setters are encapsulation"
Wrong, wrong, wrong. Encapsulation has nothing to do with getters and setters and it is very common mistake. I know there is many articles repeated it over and over all upside down...
Getters and setters don't help encapsulation even worse, they may break encapsulation. They do so when you use them to get some data from object instead of asking object to do something for you with its own data.
Encapsulation == object is taking full responsibility for its data and dosn't give it's row data. And getter on private property is nothig more than making that property public in complicated fashion == braking encapsulation.
Chceck paragraph encapsulation: http://en.wikipedia.org/wiki/C%2B%2B or http://en.wikipedia.org/wiki/Encapsulation_%28computer_science%29 Not even one word about setters or getters...
You're supposed to use the getters and setter almost everywhere, including inside the class. If you don't, then you are potentially breaking encapsulation, and even worse, you could invalidate your invariants.
As a simple example in C++:
class BankAccount
{
public:
void withdraw(int amount)
{
m_balance -= amount;
m_withdrawals++;
}
void transfer(BankAcount& other, int amount)
{
m_balance -= amount;
other.m_balance += amount;
}
private:
int m_balance;
int m_withdrawals;
};
See the bug? transfer
withdraws money, but it doesn't increment m_withdrawals
. This could have been avoided if it simply called withdraw
rather than manually decrementing the balance.
The same applies to getters and setters. For example, its quite common to have getters that lazily initialise their values. If another member function tries to access the uninitialised member directly then you're going to get a null pointer dereference.
Essentially, you should always try to use the getters and setters whenever they provide the functionality they want. The more low level things you do, the more low level bugs you are going to have. There's no point writing getters and setters if you aren't going to use them.
精彩评论