tags:

views:

157

answers:

8

I'm a novice with OOP, and I'm having trouble grasping the necessity of a getter method.

In the example:

class foo {
    $this->bar = "test";
}

$foo = new foo();
echo $foo->bar;

What are the potential pitfalls in this situation, assuming I'm using a setter method to properly validate an updated value?

+6  A: 

The value of a getter method is that it lets you change your implementation later, but keep the interface the same.

Ned Batchelder
A: 

One huge advantage of getters/setters is that you can put debug code in the functions to keep track of when values are changed and accessed when testing. Also if any formatting or processing of the variable needs to be done you can put it in the getter (or have different getters).

eCaroth
+2  A: 

Here's the classic answer:

Say you write a class with a property representing a temperature (in degrees F) and wrap it in a getTemp method, so far, not that great. Now you need to access it in degrees C as well, so you write a getTempInCelcius method that converts the temperature and returns the new version so both the old and new ways work. You then come to realise from your logs that getTempInCelcius gets far more calls than getTemp so you rewrite your class to use degrees C internally. Had you not encapsulated your temperature in getTemp then this would not be possible without changes to everything using your class. Using a getter you can just stuff the logic in there and all anyone will notice is the performance increase.

ridecar2
A: 

The getter allows greater flexiblity, so that you can change how your code works in the background while still maintaining the same interface.
For example, you could change the getter so that instead of just giving the user the data, the function actually alters it someone (decrypts it or something). To the user, they're still just calling the getter, but you have changed how it works.

chustar
A: 

It gives you more flexibility. Code can often end up needing to do something much more than you originally planned. For example, if you decide that you want $bar to be a computed value but still want $foo->getBar() to return a string.

function getBar()
{
    if(date('w')==0)
        return 'test';
    else
        return 'live';
}

If you decided you only wanted to return "test" on a Sunday (because even code deserves a day off).

Karl B
A: 

The value of accessor methods is that they allow you to have access to specific areas of a class while keeping most of it private, i.e. encapsulation. It also allows for the class signature to remain stable.

Alos
+1  A: 

There are several advantages to using getter methods. Already mentioned are formatting, keeping the external interface of the class the same even when the internals change, and debugging. I also use them sometimes for caching if you have a calculated property.

eg

class TestClass
{
    private $_cachedTestProperty;

    public function __get($varname)
    {
        switch ($varname)
        {
            case "testProperty":

                if (!isset($this->_cachedTestProperty))
                {
                    $this->_cachedTestProperty = /*calculate property*/
                }

                return $this->_cachedTestProperty;

                break;
        }
    }
}

If you do this, you will need to remember to unset the cached value if another change to the class renders it obselete

They can also provide read-only access to protected / private variables

As always with these things, whether you want to use a public property or a getter depends on what you are trying to do. They are not always better, its a case of using the right tool for the job

Addsy
A: 

A thumb-rule is that you should never expose members in your structure. Members are variables within the scope of your class.

Now, with that said you should always mark your class variables as private and create Accessors for them, which will give you a better way to expose only what needs to be exposed to the outer world.

Also when you use getters and setters you have control on the data, if you need to you can format both input and ouput.

Read more about the concepts of object oriented programmng on php.net

Filip Ekberg