How to handle "Call to a member function on a non object"
<p>How do you ensure that you don't get a "Call to a member function on a non object" fatal ?</p>

<p>Fox example, I often have something like this in my templates: (which I find very convenient and readable):</p>

<pre><code>&lt;?php echo $object-&gt;getRelatedObject()-&gt;getProperty()-&gt;formatProperty() ?&gt;
</code></pre>

<p>However, this will work only if each method returns an object of correct class. But it is not always the case. Related object may not be present in the database, so it returns null and you are faced with a fatal error. Then you go and manually check the return values:</p>

<pre><code>&lt;?php if (is_object($object-&gt;getRelatedObject()) &amp;&amp; is_object($object-&gt;getRelatedObject()-&gt;getProperty())):
  &lt;?php echo $object-&gt;getRelatedObject()-&gt;getPreperty()-&gt;formatProperty() ?&gt;
&lt;?php endif; ?&gt;
</code></pre>

<p>But this isn't so readable anymore. How do you address this problem?</p>
