I am extensively using magic methods for lots of purpose like common setters and getter, Implemented wrapper classes using __call(). Now have studied doctrine and I wondered they haven’t used magic getter and setters actually they do not work if we specify in the models.
So my questions are HOW doctrine have abbreviated magic methods? and what are the disadvantages of using magic methods specifically in ORM like docterine ?
Magic code is hard to document and understand. Usually you should add
a comment to every function and use a tool to generate a
documentation. This will not work with magic methods.
Magic methods do not support type hinting.
One of the greatest advantages of using functions to set properties is that you can use type hinting. Code like this
public function setUser(User $user){ this->user = $user }
makes sure that the user property will allways contain a valid user object or null.
IDEs do not support magic methods.
Many modern PHP IDEs support auto complete. Functions and methods are recognized by a simple parser. This parser is not able to detect magic get set or call operations.
I heard that that magic methods are slower then explicit methods but I have never tested it myself.
So how do you do it without magic?
I use templates to generate geters and setters and usually I do not use __call at all.
Magic methods are very usefull tools for creating proxy objects and and other specialized tools but you SHOULD NOT use them in every class