1

As I see it, there are three approaches to accessing object properties in PHP:

1. Magic Methods

public function __get($property) { 
  if (property_exists($this, $property)) { 
    return $this->$property; 
  } 
} 

public function __set($property, $value) { 
  if (property_exists($this, $property)) { 
    $this->$property = $value; 
  } 
} 

2. Naive Getters and Setters

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

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

3. Directly

// Set name
$dog->name = "rover"; 
// Get name
$name = $dog->name;

My question is when should these approaches be used/avoided? (ie. What are the pros and cons of each?).

Purely in terms of speed, I've done some profiling and there's large performance differences between the three. On my machine, a million accesses took on average:

Magic Methods Speed
0.58 seconds

Naive Getters and Setters Speed
0.13 seconds

Direct Access Speed
0.05 seconds

Link: Complete results

That would seem to indicate the Magic Methods should be avoided unless necessary, and the same for naive getters and setters, but (of course) that's it's only a fraction of a second per million accesses on my machine, and "premature optimization is the root of all evil", as the saying goes.

So maybe speed shouldn't be the defining factor.

Obviously you may wish to do validation on the properties, so I can see how naive getters and setters would be useful with that, too.

So when should each of these approaches be used? What situations are they best suited to?

I realise this may be closed for being primarily opinion based, but I'm taking a chance that some good answers, with logic and data to back them up, might be generated :) Let's see!

Chuck Le Butt
  • 47,570
  • 62
  • 203
  • 289
  • 2
    Naive Getters and Setters are better IMO because you can use them to do more things instead of just setting/getting values if you need to. Direct will prevent you from doing that, and the magic functions would be too much of a messy code if you need to add custom logic to a setter or getter. – apokryfos Jul 10 '17 at 12:08
  • If you want to write OOP code you should avoid these concepts at all. Let the constructor initialize all object properties. Some of them with arguments, others with default values. Inspect the code that uses the getters and move it into the class itself. Getters and setters in the form described in the question are just procedural programming disguised as OOP. – axiac Jul 10 '17 at 12:12
  • @axiac Controversial! :) Mutator methods fall under encapsulation, no? – Chuck Le Butt Jul 10 '17 at 12:56
  • You should also ask yourself if you really need getters and setters? In your quick example, I see no reason why you even need them. I often use direct access because I have no use for getters/setters. – Mikey Jul 10 '17 at 14:34
  • 1
    Possible duplicate of [Should I or should I not use getter and setter methods?](https://stackoverflow.com/questions/18351877/should-i-or-should-i-not-use-getter-and-setter-methods) – Mikey Jul 10 '17 at 14:36
  • @Mikey This isn't about whether or not to use getters and setters, it's about the pros and cons of each method to access properties. – Chuck Le Butt Jul 10 '17 at 17:01

1 Answers1

0

In my practice I use (2): private variables + getters / setters.

Direct access (3) should be avoided according to SOLID ideology.

Magic Methods (1): I suggest it can be used (IMHO) but I can remember only 1 time I needed it during 15+ years of programming practice. And even if so, the validation is desired.

Eugene Kaurov
  • 2,356
  • 28
  • 39