Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are Magic Methods Best practice in PHP? [closed]

Are Magic Methods Best practice in PHP?

like image 714
OM The Eternity Avatar asked Apr 23 '10 10:04

OM The Eternity


People also ask

Why we use magic methods in PHP?

Magic methods in PHP are special methods that are aimed to perform certain tasks. These methods are named with double underscore (__) as prefix. All these function names are reserved and can't be used for any purpose other than associated magical functionality. Magical method in a class must be declared public.

Which magic method is used to implement for loading PHP?

In PHP function overloading is done with the help of magic function __call(). This function takes function name and arguments.

What is the purpose of magic method?

Magic methods are most frequently used to define overloaded behaviours of predefined operators in Python. For instance, arithmetic operators by default operate upon numeric operands. This means that numeric objects must be used along with operators like +, -, *, /, etc.


3 Answers

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.

like image 61
Heikki Naski Avatar answered Oct 09 '22 04:10

Heikki Naski


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.

like image 24
maid450 Avatar answered Oct 09 '22 05:10

maid450


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.

like image 8
fbiville Avatar answered Oct 09 '22 06:10

fbiville