title: Accessing an object twice in string
body: <p>I've noticed this in PHP and MySQL when I've been trying to put some object values in my query. I did some google searching for "Accessing object property twice" and "-> PHP twice", but couldn't find much results. I think it's the way PHP processes strings and wanted a bit more information. This is my question:</p>
  
  <pre><code>&lt;?php
  class Object {
      public function __construct() {
          $this-&gt;a = new A();
          echo "This x is '$this-&gt;a-&gt;x'";
      }
  }
  
  class A {
      public function __construct() {
          $this-&gt;x = 1;
      }
  }
  
  $object = new Object();
  ?&gt;
  </code></pre>
  
  <p>The above code will raise an error: <code>E_RECOVERABLE_ERROR : type 4096 -- Object of class A could not be converted to string -- at line 5</code></p>
  
  <p>However, if we give it a temporary variable like this:</p>
  
  <pre><code>&lt;?php
  class Object {
      public function __construct() {
          $this-&gt;a = new A();
          $x = $this-&gt;a-&gt;x;
          echo "This x is '$x'";
      }
  }
  
  class A {
      public function __construct() {
          $this-&gt;x = 1;
      }
  }
  
  $object = new Object();
  ?&gt;
  </code></pre>
  
  <p>Then it works completely fine, or if we concatenate it, it also works fine</p>
  
  <pre><code>&lt;?php
  class Object {
      public function __construct() {
          $this-&gt;a = new A();
          echo "This x is '" . $this-&gt;a-&gt;x . "'";
      }
  }
  ?&gt;
  </code></pre>
  
  <p>Double quoted PHP strings are supposed to be able to recognize variables prefixed with <code>$</code> and automtically substitute them. This also works fine if we're only accessing the object property once (<code>-&gt;</code> single). I think the PHP double-quoted string stop processing after a single <code>-&gt;</code> thus the reason for th error:</p>
  
  <p><code>E_RECOVERABLE_ERROR : type 4096 -- Object of class A could not be converted to string -- at line 5</code></p>
  
  <p>Which makes sense if it is only being accessed once (since <code>$this-&gt;a</code> is still an object). I'm not entirely convinced and could not find much information on this in PHP, so I was wondering if anyone could elaborate or detail me more on this and why it happens?</p>