views:

239

answers:

4

Are Magic Methods Best practice in PHP?

A: 

I dont think so. My IDE is not able to show me "hints" for magic setter and getters. Altough the code is harder to debug sometimes.

I prefer not using them, bette generate needed methods (like many setters and getters) by my ide.

ArneRie
That may apply to getters and setters, but aren't `__construct`, `__clone` etc considered "magical methods", too? http://php.net/manual/en/language.oop5.magic.php How would one work without them?
Pekka
Correct agreed with @Potter i Mean @Pekka :)
OM The Eternity
+2  A: 

At least, some of these magic functions are recommended by Google. (Réf: http://code.google.com/speed/articles/optimizing-php.html, section: Avoid writing naive setters and getters). Anyway, these methods might not be performant, but they ain't deprecated at all.

Rolf
and here, another opinion: http://www.garfieldtech.com/sites/default/files/benchmarks.png :D(see http://www.garfieldtech.com/blog/magic-benchmarks)
Rolf
+3  A: 

I don't think magic methods are best or worst practice: depending on what you want to achieve you can use them or not... What I mean is that you don't have to tweak your code as possible to use them, but if you have to there is no problem at all.

If you have an object with 3 and only 3 attributes you don't need to use magic setters/getters, but in some advanced cases they are a great way to do very complex things (ORM systems etc...)

Maybe some of them are deprecated, I don't know, but most of them are not.

maid450
+1  A: 

cons

  1. Text searches don't find the functions

  2. System is harder to understand, especially for newcomers

  3. Refactoring tools might fail more often

Generally, the magic methods do things behind the scenes and the programmer might not realize it's happening which makes debugging harder.

Heikki Naski