title: PHP OOP a lot of setters, getters
body: <p>I need to create approx. 5-7 classes, every class will contain a lot of members (let us say each class will contain 20 members). I could create them using public access, like:</p>
  
  <pre><code>class A {
      public $myPropertyOne = '';
      public $myPropertyTwo = '';
      ...
  }
  </code></pre>
  
  <p>My preferred way of course to make these members private and create get/set methods for each property. I.e.</p>
  
  <pre><code>class A {
      private $myPropertyOne = '';
      private $myPropertyTwo = '';
  
      public function getMyPropertyOne() {
              return $this-&gt;myPropertyOne;
      }
  
      public function setMyPropertyOne($myPropertyOne) {
              $this-&gt;myPropertyOne = $myPropertyOne;
      }
  
      public function getMyPropertyTwo() {
              return $this-&gt;myPropertyTwo;
      }
  
      public function setMyPropertyTwo($myPropertyTwo) {
              $this-&gt;myPropertyTwo = $myPropertyTwo;
      }
  }
  </code></pre>
  
  <p>But considering a class will have 20 properties, I will have in addition to this add 40 methods. And my concern here is how will this slow down the script and much more memory this will require (remember I am going to have several classes like this).</p>
  
  <p>Another solution could be to use magic functions __set, __get but I don't want to, because the code completion in development IDE will not suggest properties which is crucial for me.</p>
  
  <p>If this would be a compiled language (like C++) I would not have a question and would use the solution with getters, setters but since the PHP is interpreted language I am interested in my scripts to use less RAM and be as fast as possible.</p>
  
  <p>Thanks in advance, any thoughts regarding this question would be much appreciated!</p>
  
  <hr>
  
  <p><strong>My Opinion</strong></p>
  
  <p>Thank you all for your answers, I just wanted to share my opinion in case someone will look for an answer to this question.</p>
  
  <p>I cannot fully agree with those who say that you should not care about performance as this is task of optimizers, I think this is important factor (well atleast as for me), when we're dealing with interpreted language such as PHP we will always have to think about memory and speed (this all reminds me the time when I was developing system apps for DOS, heh :) and you always have been limited with poor CPU and kilobytes of total RAM so you got happy if you could save an additional byte), in PHP development you have the same picture as regardless of how many server you add, users' count will be always higher so that you always have to decide if you want to follow classic/safe/proper method or to avoid this and get some gain in speed or memory. </p>
  
  <p>So.... my opinion is that the best way here is to use public access for all member and avoid getters/setters for all properties and use private access with get/set methods for properties which requires data validation or initialization before a value will be set.</p>
  
  <p>For example:</p>
  
  <pre><code>class B {
      public $myPropertyOne = '';
      public $myPropertyTwo = '';
      private $myPropertyThree = array();
  
  
      public function getMyPropertyThree() {
          return $this-&gt;myPropertyThree;
      }
  
      public function setMyPropertyThree($val) {
          if(!is_array($val)) {
              return;
          }
  
          $this-&gt;myPropertyThree = $val;
      }
  }
  </code></pre>
  
  <p>Thank you for spending time on my question!</p>