title: PHP quick syntax for creating an stdClass with pre-populated data
body: <p>I should add out by saying that while KVP-arrays work fine, I prefer objects when doing OOP as <code>$foo-&gt;bar-&gt;foo</code> just seems cleaner than <code>$foo-&gt;bar['foo']</code> in my opinion.</p>
  
  <p>PHP has a great way of creating arrays with pre-populated data through <code>$foo = array('foo' =&gt; 'bar');</code> or even the <a href="https://wiki.php.net/rfc/shortsyntaxforarrays" rel="nofollow">new 5.4 bracket syntax</a>: <code>$foo = ['foo' =&gt; 'bar']</code>, but the same syntax does not seem to exist for objects (<code>stdClass</code>).</p>
  
  <p><a href="http://codepad.org/ZxQc3y8X" rel="nofollow">Array demo</a>:</p>
  
  <pre><code>&lt;?php
      class Foo {
          public $bar = array(
              'foo' =&gt; 'bar',
              'bar' =&gt; 'foo'
          );
      }
  
      $foo = new Foo;
      var_dump($foo-&gt;bar);
      /*
          array(2) {
            ["foo"]=&gt;
            string(3) "bar"
            ["bar"]=&gt;
            string(3) "foo"
          }
      */
  ?&gt;
  </code></pre>
  
  <p>Great - what if we want to do the same with objects <em>without using a <code>__construct</code></em>?</p>
  
  <hr>
  
  <p><a href="http://codepad.org/wnlH7w1N" rel="nofollow"><strong>Try #1 - casting to object</strong> - nope; we can't cast in the declaration of a class variable</a>:</p>
  
  <pre><code>&lt;?php
      class Foo {
          public $bar = (object)array(
              'foo' =&gt; 'bar',
              'bar' =&gt; 'foo'
          );
      }
  
      /*
          Parse error: syntax error, unexpected T_OBJECT_CAST on line 4
      */
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p><a href="http://codepad.org/VqbnRm5A" rel="nofollow"><strong>Try #2 - using <code>json_decode</code> and <code>json_encode</code></strong>  - nope; we can't call functions in the declaration of a class variable</a>:</p>
  
  <pre><code>&lt;?php
      class Foo {
          public $bar = json_decode(json_encode(array(
              'foo' =&gt; 'bar',
              'bar' =&gt; 'foo'
          )));
      }
  
      /*
          Parse error: syntax error, unexpected '(', expecting ',' or ';' on line 3
      */
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p><a href="http://codepad.org/KaO3TifR" rel="nofollow"><strong>Try #3 - using javascript style brackets</strong> - nope; even though <code>[]</code>'s were added in PHP 5.4, object brackets still do not exist*</a>:</p>
  
  <p>*<a href="https://wiki.php.net/rfc/shortsyntaxforarrays" rel="nofollow"><em>rfc for array brackets</a></em></p>
  
  <pre><code>&lt;?php
      class Foo {
          public $bar = {
              'foo' =&gt; 'bar',
              'bar' =&gt; 'foo'
          };
      }
  
      /*
          Parse error: syntax error, unexpected '{' on line 3
      */
  ?&gt;
  </code></pre>
  
  <hr>
  
  <p>The only way that seems to work is by using a <code>__construct</code> and converting the KVP-array to an object, but it just seems completely backwards to declare a variable as one thing, and before we ever use it, cast it to something else.</p>
  
  <p><a href="http://codepad.org/LgkaGFWP" rel="nofollow"><strong><code>__construct</code> demo:</strong></a></p>
  
  <pre><code>&lt;?php
      class Foo {
          public $bar = array(
              'foo' =&gt; 'bar',
              'bar' =&gt; 'foo'
          );
  
          public function __construct() {
              $this-&gt;bar = (object)$this-&gt;bar;
          }
      }
  
      $foo = new Foo;
      var_dump($foo-&gt;bar);
      /*
          object(stdClass)#2 (2) {
            ["foo"]=&gt;
            string(3) "bar"
            ["bar"]=&gt;
            string(3) "foo"
          }
      */
  ?&gt;
  </code></pre>
  
  <hr>
  
  <ul>
  <li>Is there some short-hand syntax I haven't been able to find?   </li>
  <li>If
  not; is it going to be added in future PHP versions?   </li>
  <li>If not/until
  then; Is there a better way than having to add <code>__construct</code>'s every
  single time?   </li>
  <li>Should I, instead, just deal with the fact that using
  an array in this situation is better, even though, <em>in my opinion</em>,
  it seems messier?</li>
  </ul>
  
  <p><strong>Why?</strong>:</p>
  
  <p>Working on a new DB class for a company, I've had to resort to the current code in order to manage saving DB credentials for further inspection later in the code. Of course a different design pattern, such as <code>$connectionHost</code>, <code>$connectionDatabase</code>, etc. would work fine - but it seems cluttered to me.</p>
  
  <p><a href="http://codepad.org/vwy1y9NP" rel="nofollow">Example</a>:</p>
  
  <pre><code>&lt;?php
      class DB {
          public $connection = array(
              'host'     =&gt; null,
              'database' =&gt; null,
              'user'     =&gt; null,
              'engine'   =&gt; null
          );
          public function __construct($host, $database, $user, $engine = 'mysql') {
              //Essentially I'd like the following line not to be needed:
              $this-&gt;connection = (object)$this-&gt;connection;
  
              $this-&gt;connection-&gt;host     = $host;
              $this-&gt;connection-&gt;database = $database;
              $this-&gt;connection-&gt;user     = $user;
              $this-&gt;connection-&gt;engine   = $engine;
          }
      }
  
      $db = new DB('127.0.0.1', 'server_db', 'my_user');
      var_dump($db-&gt;connection-&gt;host);
  ?&gt;
  </code></pre>
  
  <p><a href="http://codepad.org/vwy1y9NP" rel="nofollow">http://codepad.org/vwy1y9NP</a></p>