title: PHP: how to resolve a dynamic property of an object that is multiple levels deep
body: <p>I am have written a helper function to "cleanup" callback variables for input into MySQL. This is the function that I wrote:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function string($object, $objectPath) {
      if (!empty($object-&gt;$objectPath) || $object-&gt;$objectPath !== '') {
          $value = $object-&gt;$objectPath;
      } else {
          return 'NULL';
      }
      if (!empty($value) || $value != '') {
          return "'".str_replace("'","''",$value)."'";
      } else {
          return 'NULL';
      }
  }
  </code></pre>
  
  <p>Now, <code>$object</code> is always an object returned by the call, and <code>$objectPath</code> is always a string to points to a given value. Here's where the problem comes in. This works:</p>
  
  <pre><code>$value = $this-&gt;db-&gt;string($object, 'foo');
  </code></pre>
  
  <p>However, this does not work:</p>
  
  <pre><code>$value = $this-&gt;db-&gt;string($object, 'foo-&gt;bar-&gt;foo1-&gt;bar1');
  </code></pre>
  
  <p>Whenever <code>$objectPath</code> is more than "one layer" deep, I get the following error from (Amazon's) client library:</p>
  
  <pre><code>Fatal error:  Call to undefined method MarketplaceWebServiceOrders_Model_Order::getFoo-&gt;Bar() in /path/to/Model.php on line 63
  </code></pre>
  
  <p>The code block that the error refers to is this:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function __get($propertyName)
      {
         $getter = "get$propertyName"; 
         return $this-&gt;$getter(); // this is line 63
      }
  </code></pre>
  
  <p><code>$object</code> is not XML, so I can't use <code>SimpleXMLElement</code> and <code>XPath</code>.</p>
  
  <p>What is the problem with my code? Is it that am I concatenating an object and a string? If so, how can I make that possible? How can I get this function to do what I intended it to do?</p>
  
  <p>By the way, I'm using PHP 5.4.27.</p>