views:

203

answers:

2

when using doctrine i stumble upon these 2 words: accessor and mutator.

are these only used in doctrine or are they specific for php?

and what do they mean?

thanks

+7  A: 

They're just fancy terms for getters and setters.

class MyClass
{
    private $prop;

    // Accessor (or Getter)
    public function getProp()
    {
        return $this->prop;
    }


    // Mutator (or Setter)
    public function setProp($value)
    {
        $this->prop = $value;
    }

}
webbiedave
And to clarify, they're common terms used in Object Oriented programming, no matter the language or framework.
nickf
so why do they call them accessors and mutators instead of getters and setters? they aren't magic methods, that is to say, they if i type setProp() but haven't defined one method for that, it won't be created automatically?
never_had_a_name
@fayer accessor and mutator are more general terms used in computer science. getter and setter are more casual way I guess... maybe a slang. As long as I know, it won't magically create the variables for you in php... need to look the specifications... pretty sure it won't.
m0s
+1  A: 

If I understand you correctly these 2 are specific to I guess any object oriented programming language. The point is that accessor is a method or a function which provides access to private fields in your class and mutator method allows to modify the private fields. I can go on writing about this, but I suggest you just google these, and you'll get lots of information about it. Its all about encapsulation <- suggest you to look up that term as well.

m0s
I like to think of them as regular functions that just serve some common well known purpose... nothing more.
m0s