Can PHP dissect its own syntax?
<p>Can PHP dissect its own syntax? For example, I'd like to write a function that takes in an input like <code>$object-&gt;attribute</code> and says to itself:</p>

<blockquote>
  <p>OK, he's giving me <code>$foo-&gt;bar</code>, which means he must think that <code>$foo</code> is an object that has a property called <code>bar</code>. Before I try accessing <code>bar</code> and potentially get a 'Trying to get property of non-object' error, let me check whether <code>$foo</code> is even an object.</p>
</blockquote>

<p>The end goal is to <strong>echo a value if it is set, and fail silently if not</strong>.</p>

<p>I want to avoid repetition like this:</p>

<pre><code>&lt;input value="&lt;? if(is_object($foo) &amp;&amp; is_set($foo-&gt;bar)){ echo $foo-&gt;bar; }?&gt; "/&gt;
</code></pre>

<p>...and to avoid writing a function that does the above, but has to have the object and attribute passed in separately, like this:</p>

<pre><code>&lt;input value="&lt;? echoAttribute($foo,'bar') ?&gt;" /&gt;
</code></pre>

<p>...but to <b>instead</b> write something which:</p>

<ul>
<li>preserves the object->attribute syntax</li>
<li>is flexible: can also handle array keys or regular variables</li>
</ul>

<p>Like this:</p>

<pre><code>&lt;input value="&lt;? echoIfSet($foo-&gt;bar); ?&gt; /&gt;
&lt;input value="&lt;? echoIfSet($baz['buzz']); ?&gt; /&gt;
&lt;input value="&lt;? echoIfSet($moo); ?&gt; /&gt;
</code></pre>

<p>But this all depends on PHP being able to tell me "what kind of thing am I asking for when I say <code>$object-&gt;attribute</code> or <code>$array[$key]</code>", so that my function can handle each according to its own type.</p>

<p><strong>Is this possible?</strong></p>

<h2>Update</h2>

<p>I got some good answers here and did some experimenting. Wanted to summarize.</p>

<ul>
<li>The answer to my original question appears to be "no," but we did find a way to accomplish was I was trying to do. <strong>Techpriester</strong> pointed out that I <em>could</em> pass the string '$foo->bar' to a function and have it parse that and <code>eval()</code> it. Not the approach I want to take, but deserves a mention.</li>
<li><strong>Peter Bailey</strong> pointed out that something like <code>$foo-&gt;bar</code> gets evaluated before getting passed to a function. I should have thought of this, but for some reason didn't. Thanks, Peter!</li>
<li><strong>Will Vousden</strong> showed that passing <code>$foo-&gt;$bar</code> by reference allows the function to evaluate it, rather than having it evaluated beforehand. His function shows that <code>isset($foo-&gt;bar)</code> will not complain if <code>$foo</code> is not an object, which I didn't know.</li>
</ul>

<p>More interestingly, his example seems to imply that <strong>PHP waits to evaluate a variable until it absolutely has to.</strong></p>

<p>If you're passing something <strong>by value</strong>, then of course PHP has to determine <strong>the value</strong> right then. Like this:</p>

<pre><code>$foo-&gt;bar = 'hi';
somefunction($foo-&gt;bar); // same as somefunction('hi');
</code></pre>

<p>But if you're passing <strong>by reference</strong>, it can wait to determine the value when it actually needs to.</p>

<p>Like this (following the order in which things happen):</p>

<pre><code>echo ifSet($foo-&gt;bar); // PHP has not yet evaluated $foo-&gt;bar...
function ifSet(&amp;$somevar){ // ...because we're passing by reference (&amp;$somevar)
  // Now we're inside the function, but it still hasn't evaluated $foo-&gt;bar; it 
  // just made its local variable, $somevar, point to the same thing as $foo-&gt;bar
  if(isset($somevar)){ // Right HERE is where it's evaluated - when we need it
    return $bar;
  }
}
</code></pre>

<p>Thanks to everyone who responded! If I've misstated something, please let me know.</p>
