13

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

nickf
  • 537,072
  • 198
  • 649
  • 721
never_had_a_name
  • 90,630
  • 105
  • 267
  • 383

4 Answers4

28

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
  • 48,414
  • 8
  • 88
  • 101
  • 4
    And to clarify, they're common terms used in Object Oriented programming, no matter the language or framework. – nickf May 04 '10 at 06:07
  • 1
    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 May 04 '10 at 06:14
  • 1
    @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 May 04 '10 at 06:24
3

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
  • 4,250
  • 9
  • 41
  • 64
  • I like to think of them as regular functions that just serve some common well known purpose... nothing more. – m0s May 04 '10 at 06:30
3

Shouldn't we be using __get and __set in php5 and later now?

Musa Haidari
  • 2,109
  • 5
  • 30
  • 53
DssTrainer
  • 1,439
  • 3
  • 12
  • 15
1

They aren't just different terms for getters and setters, at least not in Laravel.

To quote the documentation: "Accessors and mutators allow you to format Eloquent attributes when retrieving them from a model or setting their value."

https://laravel.com/docs/master/eloquent-mutators

So, you could say that getters and setters are a subset of accessors and mutators which change the data by a factor of zero.

Put it another way, if I wanted to get a raw field "cost" from a table, I would use a getter. If I wanted to get that field expressed and formatted in pounds and pence, then I could use an accessor.

There are other ways I could go about it, but that's one option.

Relaxing In Cyprus
  • 1,976
  • 19
  • 25
  • Could you please explain "changing by a factor of zero"? Thanks! – Zsolt Szilagyi Sep 20 '17 at 10:20
  • Sure. Accessors and Mutators change a value before either saving or returning the new value. Setters and get it don't change the value. So when I said "by a factor of zero" I just meant "no change". You have just added nothing to the value. – Relaxing In Cyprus Sep 26 '17 at 10:35