Getter/Setter for every domain model's attributes mandatory?

Is it mandatory to have a getter and a setter for every domain model class’ attribute? Does Flow (for technical reasons - e.g. property mapper) need to have a (very basic) getter and a setter for every domain model class’ attribute?

In some situations I do not feel the need to implement a getter and/or setter for specific domain model class’ attributes.

Example 1: a specific attribute is (only) set via constructor and should then be immutable.
Example 2: a specific attribute is not meant to be output by itself but only as a factor of another getter (“computed property”).

Sometimes I also feel the need to implement a getter for a attribute foo that has some more logic than return $this->foo. Could this lead to problems due to the fact, that Flow by all means needs a very basic getter for every single attribute?

Note: I have the impression that TYPO3 CMS’ Extbase (for technical reasons) needs a getter and a setter for every domain model class’ attribute to work reliably/smoothly in some situations.

Flow does not require any getter or setter.
When you want to use the property mapper, you need to provide setters for the properties you want to be mapped.

I’m not sure if the policy uses reflection, probably not. Then you need to have getters for everything you want to check in the policy (in method privileges, entity privileges work on SQL directly).

Other than that, working with DTOs really help a lot in keeping your model clean.

1 Like

I don’t know if there must be a getter and setter for every attribute but regards the computed values - you can always add another getter.
E.g. I have a model Purchase that has attributes amount and itemPrice. Then I have a getter getPriceTotal() that returns $this->amount*$this->itemPrice (it’s a bit more complicated but in general that’s the logic).
This can then also be used in Fluid views with {purchase.priceTotal}.

Don’t know if that helps you though :smile:

Isn’t the property mapper generally used as soon as you use a form to create/update an object?

@akii Even if you noted at the beginning that Flow does not require any getter or setter it may be safer to provide a getter and a setter for each attribute (at least as soon as you work with forms and/or the policy), right?

Adding getters (e.g. for “computed properties”) works for sure.

I only wondered if you can omit one of those basic return $this->foo getters if you do not use it anywhere, because some magic functionality of Flow depends on the existence of getters/setters for every domain model’s attribute.

“Magic” things concerning getters and setters you encounter in Flow are basically those:

  • Property Mapper (Forms)
  • Policy
  • Fluid

I personally try to avoid getters / setters wherever I can (you can even write models without any getter or setter), although when you do not apply DDD you might as well just generate them. You can take a look at the usages of ObjectAccess, which is used within Flow to set and get properties. That should give you a good understanding where it is used.

1 Like