People have to rediscover "magic" setters/getters every now and then, and write articles praising them, until one day they have to debug the code where some silly programmer who read a lot of articles like that abused those magic methods so your head would explode on why your house gets automatically sold online when you set wrong amount of wood in the fireplace. Then they realize how evil this unobvious logic is.

If you need to use validation on setting a value, you better just use a function, so that later it would be obvious there is something else happening when you set a value.

car.set_fuel_amount(...) is much better at reading it and using it, than car.fuel_amount=... that throws an exception whenever it feels like it.

Worse you have to *know* that you need to process exceptions for setting one value, when another might be just a regular object field.

tl;dr: avoid using magic functions in favor of regular functions, except when it is a one-off thing that does not affect much and fails gracefully.

--

Drupal Technical Lead, Docksal creator and maintainer

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Oleksii Chekulaiev

Oleksii Chekulaiev

Drupal Technical Lead, Docksal creator and maintainer