title: Using generic getters and setters with php
body: <p>There's thousands of examples of php __get and __set out there, unfortunately nobody actually tells you how to use them.</p>
  
  <p>So my question is: how do I actually call the __get and __set method from within the class and when using an object.</p>
  
  <p>Example code:</p>
  
  <pre><code>class User{
  public $id, $usename, $password;
  
  public function __construct($id, $username) {
           //SET AND GET USERNAME
  }
  
  public function __get($property) {
      if (property_exists($this, $property)) {
          return $this-&gt;$property;
      }
  }
  
  public function __set($property, $value) {
      if (property_exists($this, $property)) {
          $this-&gt;$property = $value;
      }
  
      return $this;
  }
  }
  
  $user = new User(1, 'Bastest');
  // echo GET THE VALUE;
  </code></pre>
  
  <p>How would I set the values in the constructor and how would i get the value in the <code>// echo GET THE VALUE;</code></p>