Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use of getter-setter within class

Should one use under any circumstances getters-setters of a class within the class?

like image 650
Argiropoulos Stavros Avatar asked Jan 22 '11 10:01

Argiropoulos Stavros


2 Answers

@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...

like image 83
smentek Avatar answered Nov 15 '22 23:11

smentek


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.

like image 29
Peter Alexander Avatar answered Nov 15 '22 22:11

Peter Alexander