title: PHP: change order of object properties
body: <p>Regarding PHP, is there a way to change the order of object properties?</p>
  
  <pre><code>class o {public $a = 1, $b = 2;}
  $o = new o;
  foreach (get_object_vars($o) as $k =&gt; $v) {
    print $k . '-&gt;' . $v . PHP_EOL;
  }
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code>a-&gt;1
  b-&gt;2
  </code></pre>
  
  <p>Existing public variables can be <code>unset()</code>, and added e.g. with <code>$o-&gt;c = 3;</code>. But <a href="https://secure.php.net/manual/en/ref.array.php" rel="nofollow noreferrer">array functions</a> do not work with objects, and I do not want to convert the object to some stdClass.</p>
  
  <p>The only practical workaround I can think of is to decorate an array object and overload the magic <code>__get()</code> and <code>__set()</code> methods, but that is just a workaround, not a solution.</p>