0

I'm working on a personal project in PHP and like most classes that are built, I require getter/setter functions.

I had a thought and did some research but couldn't find an answer. Instead of defining two functions - one get and one set - why couldn't they just be handled by a single function?

function myVar ($newVar = NULL) {
   if(isset($newVar)) {
     $this->var = $newVar;
   } else {
      return $this->var;
   }
}

Are there any downsides to this that I may not be seeing?

attiliov
  • 41
  • 1
  • 7
  • 1
    What if you actually want to set a value to `null`? There's a downside..... is having two magic methods really so very complicated? – Mark Baker Apr 10 '15 at 19:42
  • Why not just have the property on the class? I.e. `public $var;` at the top, then you can just call `$class->var = "hello"` to set or `$class->var` to get. – James Spence Apr 10 '15 at 19:48
  • 1
    Also, why would you want to add an "if" to every single get or set operation? When you want to read, you know you want to read, when you want to write, you know you want to write, having the code decide for you would be confusing. – Dzyann Apr 10 '15 at 19:52
  • If you know a variable needs to be NULL for some reason then you can adjust the code accordingly. Although that is a good point. Magic methods and public variables don't allow for validation when setting. Sure that could be handled before actually setting it, but I don't see the point of that. I'd rather just have a get/set for each. – attiliov Apr 10 '15 at 20:03

1 Answers1

2

You could use __get and __set magic dust. But, there is a downside: you'll lose IDE auto completion, PHPDoc generation, inheritance. It helps not to write code, but it's not clean, you don't have public/protect/private logic. It's the same for your method.

class MyClass {
  private $one;
  private $two;

  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;
    }

    return $this;
  }
}
halfer
  • 19,824
  • 17
  • 99
  • 186
danielpopa
  • 810
  • 14
  • 27
  • You can add IDE auto-complete back in using `@method` comments, I believe. – halfer Apr 10 '15 at 19:46
  • You don't lose auto completion in all cases. in PHPStorm you can add properties via the PHPDoc for the class in order to have autocomplete entries for variables. EDIT: As @halfer said you can also do it with methods. – James Spence Apr 10 '15 at 19:46
  • Use [@property](http://www.phpdoc.org/docs/latest/references/phpdoc/tags/property.html) in your docblocks – Mark Baker Apr 10 '15 at 19:46
  • I'm actually not a fan of magic methods. I prefer a method for each variable, especially if that variable requires some form of validation. – attiliov Apr 10 '15 at 20:01
  • @attiliov `I prefer a method for each variable`.... yet you're wanting to use a single method for both setting and getting every variable.... isn't that a bit of a contradiction? – Mark Baker Apr 10 '15 at 21:03
  • I guess in a way it is a contradiction. Perhaps I've been using jQuery way too much which is why I'm having trouble finding a downside to this approach. – attiliov Apr 11 '15 at 11:43