tags:

views:

452

answers:

1

In this document, it explains how to use mutators and accessors in Doctrine, but does not explain what they are.

Could anyone explain what mutators and accessors do and what they are?

Thanks in advance.

+3  A: 

You can use mutators and accessors to implement additional behavior for your models' fields. Basically they transform the value from one form into another. For example if you look at Doctrine's docs they specify a md5Password mutator. Mutator means that Doctrine will call the specified mutator method whenever you set the value for the field. So whenever you do:

$user->password = 'foobar';

Doctrine will call the md5Password() of the model, hence transforming 'foobar' into md5('foobar'). In a nutshell this ensures that the password is always encrypted on software-level.

Accessor is the opposite of mutator; it'll be called when the field is being read instead of being set (eg. when a row is read from the database).

reko_t