35

Are Magic Methods Best practice in PHP?

OM The Eternity
  • 14,004
  • 37
  • 114
  • 174
  • 1
    What do you mean by "best practice"? Your question doesn't really make sense to me. They are tools, some things can't be done without them. What exactly do you want to know? – Pekka Apr 23 '10 at 10:56
  • I was reading a book n OOP I encountered the getter and setter where I saw the magic method terminology.. But once someone told me they are deprecated, is it so ?? – OM The Eternity Apr 23 '10 at 10:58
  • 2
    My guess is either he thinks they are sneaky/cheating (like `i++` and `goto` or he's worried they are overused or unreliable. y'know, like using tables for styling. – Anthony Apr 23 '10 at 10:59
  • Ohh I guess That what we call Property Overloading isnt it? – OM The Eternity Apr 23 '10 at 11:00
  • @Parth please add some detail about which methods are viewed as deprecated. – Pekka Apr 23 '10 at 11:47
  • @Pekka not specified, developers senior to me suggested me to avoid using the methods.. but as u said __construct and __clone are not those.. – OM The Eternity Apr 23 '10 at 11:57
  • `goto` is almost guaranteed code smell. It is not being sneaky—it is being insane. – CommandZ Apr 10 '14 at 15:09
  • There's a much better series of answers here: https://stackoverflow.com/questions/6184337/best-practice-php-magic-methods-set-and-get – Chuck Le Butt Jun 01 '17 at 15:26

4 Answers4

41

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.

When searching for the functions (or other symbols) can't find all the matches it becomes a nightmare to remove old code and this fear can cause dead code to pile up in the codebase. If the dead code is removed, it can cause breakage in unknown places.

Heikki Naski
  • 2,070
  • 1
  • 17
  • 13
24

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
  • 7,338
  • 3
  • 33
  • 32
7

At least, some of these magic functions are recommended by Google:

Avoid writing naive setters and getters

When writing classes in PHP, you can save time and speed up your scripts by working with object properties directly, rather than writing naive setters and getters. In the following example, the dog class uses the setName() and getName() methods for accessing the name property.

class dog {
  public $name = '';

  public function setName($name) {
    $this->name = $name;
  }

  public function getName() {
    return $this->name;
  }
}

Notice that setName() and getName() do nothing more than store and return the name property, respectively.

$rover = new dog();
$rover->setName('rover');
echo $rover->getName();

Setting and calling the name property directly can run up to 100% faster, as well as cutting down on development time.

$rover = new dog();
$rover->name = 'rover';
echo $rover->name;

Original link: http://code.google.com/speed/articles/optimizing-php.html

Archived version: https://web.archive.org/web/20120208060457/http://code.google.com/speed/articles/optimizing-php.html

Anyway, these methods might not be performant, but they ain't deprecated at all.

Chuck Le Butt
  • 43,669
  • 58
  • 179
  • 268
fbiville
  • 6,836
  • 5
  • 48
  • 72
  • 2
    and here, another opinion: http://www.garfieldtech.com/sites/default/files/benchmarks.png :D (see http://www.garfieldtech.com/blog/magic-benchmarks) – fbiville Apr 23 '10 at 12:01
  • 2
    This answer is completely off-topic. "Google" does not go against using native getters/setters vs. magic functions. They advise against using native getters/setters if they only directly modify the property. This is not the same thing. – Sébastien Renauld Aug 26 '13 at 10:28
  • They go against using "naive" getters/setters. The popular answers to the initial question at that time (3 years ago, btw, here is my present to you: http://xkcd.com/386/) were suggesting magic methods were deprecated. My answer was just making a point that this was not necessarily an absolute truth. – fbiville Aug 27 '13 at 09:05
  • IDE support should be improved to prevent warnings telling method 'x' doesn't exists when a magic function exists ... – Antoine Marques Mar 02 '18 at 08:12
3

I don't 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, better generate needed methods (like many setters and getters) by my ide.

YakovL
  • 5,213
  • 10
  • 46
  • 71
opHASnoNAME
  • 18,735
  • 24
  • 93
  • 138
  • 2
    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 Apr 23 '10 at 11:02
  • Correct agreed with @Potter i Mean @Pekka :) – OM The Eternity Apr 23 '10 at 11:06