0

It is a bad way of programming, when i get access to my private/protected class members directly in the class via my getter/setter methods?

Alternative #1

<?php

class A {
    private $myVariable;

    public function getMyVariable() {
        return $this->myVariable;
    }

    public function doSomething() {
        $variable = $this->getMyVariable();
    }
}

?>

Alternative #2

<?php

class A {
    private $myVariable;

    public function doSomething() {
        $variable = $this->myVariable;
    }
}
?>

Which way do you prefer? I think the first solution is more readable in constrast to the second one. Please let me hear your opinions.

Thanks in advance.

xyNNN
  • 492
  • 3
  • 21
  • in my opinion method #2 is perfectly fine. The variable is private. The getter/setter is there in other languas to control the access from external classes. Since you are just have to perform `doSomething()` the shorter version should be enougth. – Friedrich Aug 18 '14 at 06:46
  • possible duplicate of [Why use getters and setters?](http://stackoverflow.com/questions/1568091/why-use-getters-and-setters) – Noy Aug 18 '14 at 06:47
  • No! This isn't my question! I know why to use getter and setter from outside my class, to scope my variables - but this is not my question. – xyNNN Aug 18 '14 at 06:47
  • 1
    LOOK AT THE ACCEPTED ANSWER – Noy Aug 18 '14 at 06:48
  • It depends. If we're talking about the example above, then #2, because there's less function calls. But if the getter function served more purpose such as validation, [as explained here](http://stackoverflow.com/a/1568230]), then it would be more beneficial to use your function to retrieve the variable. – Dave Chen Aug 18 '14 at 06:50
  • Thank you Noy Gabay, i didn't looked at the accepted answer. Then my solution #1 is the best way and this was also my suspicion. – xyNNN Aug 18 '14 at 06:51

2 Answers2

1

Since you are determined this is not a duplicate, I will copy the points from this response relevant to this case:

  • Encapsulation of behavior associated with getting or setting the property - this allows additional functionality (like validation) to be added more easily later.
  • Controlling the lifetime and memory management (disposal) semantics of the property - particularly important in non-managed memory environments (like C++ or Objective-C).
  • Providing a debugging interception point for when a property changes at runtime - debugging when and where a property changed to a particular value can be quite difficult without this in some languages.
  • Allowing inheritors to change the semantics of how the property behaves and is exposed by overriding the getter/setter methods.
Community
  • 1
  • 1
Noy
  • 1,258
  • 2
  • 11
  • 28
  • Maybe you could explain these points in detail? I kinda lost it at `Controlling the lifetime and memory management`. – Dave Chen Aug 18 '14 at 06:56
0

Maybe you should think in another way: Why do we need getter/setter? They abstract direct access to a field. Even if the Getter does nothing other than setting a value, it can protect you later on. Changing a field to a Getter later is a breaking change, especially in PHP IDEs (badumm). So I think whenever you want to protect a field to prevent vulnerable/buggy code, use getter/setter.

David Leitner
  • 3,312
  • 1
  • 18
  • 27