__get __set or individuals getters and setters? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

__get __set or individuals getters and setters?

In PHP 7, is a better programming practice to use magic methods for getters and setters or setting them individually for each attribute?

21st Apr 2018, 9:14 PM
Thiago Martins Saraiva
Thiago Martins Saraiva - avatar
1 Answer
0
After 1 year thinking about it, I found the conclusion that... If there is no business logic behind the get or set method to work, there is no difference. But, about clean code, best practices, haven't found resources about it. I would recommend to use the magic methods for getting and setting the attributes, because that way, you write once and reuse as much as you need. But, if there is a business logic behind, like you can only set the age if it's greater than 18, you are going to need an individual set method.
12th Jul 2019, 2:38 AM
Thiago Martins Saraiva
Thiago Martins Saraiva - avatar