Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>XML has support for embedding so called <a href="http://www.w3.org/TR/REC-xml/#sec-pi" rel="nofollow noreferrer"><em>Processing Instructions</em> (PI)</a>, a valid XML could look like this:</p> <pre><code>&lt;doThis&gt;&lt;?php doThis() { bang bang bang! } ?&gt;&lt;/doThis&gt; </code></pre> <p>which is pretty similar to what you know from the PHP language. However support only means, that you can put processing instructions in there but the processing of those instructions is not of the XML's business.</p> <p>Also the XML parser you use - <a href="http://php.net/SimpleXML" rel="nofollow noreferrer">SimpleXML</a> - has no dedicated support for processing instructions. You can use <a href="http://php.net/DOMDocument" rel="nofollow noreferrer">DOMDocument</a> for that instead. So much from the XML side.</p> <p>For the PHP side you have got the problem that you somehow define a function while you want to call it instead. Also the PHP code is invalid in other parts (probably because you just put together a quick question). Maybe this is better:</p> <pre><code>&lt;doThis&gt;&lt;?php function doThis() { return 'bang bang bang!'; } ?&gt;&lt;/doThis&gt; </code></pre> <p>With some little DOM based parser (here called <code>PiDOMDocument</code>) this can be turned into a class/object:</p> <pre><code>$doc = new PiDOMDocument(); $doc-&gt;loadXML($xml); $obj = $doc-&gt;getObject(); echo $obj-&gt;doThis(); </code></pre> <p>The output of <code>echo</code> in the last line then is:</p> <pre><code>bang bang bang! </code></pre> <p>(quelle surprise). You could internally map this into a SimpleXMLElement to streamline that into your example code above:</p> <pre><code>$foo = simplexml_load_string($xml, 'PiSimpleXMLElement'); echo $foo-&gt;doThis(); //bang bang bang! </code></pre> <p>This works like:</p> <pre><code>/** * SimpleXMLElement integration of PiDOMDocument */ class PiSimpleXMLElement extends SimpleXMLElement { public function __call($name, $args) { $doc = new PiDOMDocument(); $doc-&gt;loadXML($this-&gt;asXML()); $callback = array($doc-&gt;getObject(), $name); return call_user_func_array($callback, $args); } } </code></pre> <p>As PHP processor you can take <code>eval</code> or <code>include</code> (<a href="https://stackoverflow.com/q/1184628/367456">which basically is both the same</a>). PHP is executed directly so if your code has fatal parse errors, the overall script has fatal errors, if you do not like this PHP default behavior, you can parse the string before eval'ing it for such errors, see as well <a href="http://php.net/token_get_all" rel="nofollow noreferrer"><code>token_get_all()</code></a> and <a href="https://stackoverflow.com/a/7874314/367456">this example</a> or <a href="https://stackoverflow.com/a/3268443/367456">this example</a>.</p> <p>I opted for an unvalidated <code>eval</code> in my <a href="http://eval.in/9908" rel="nofollow noreferrer">online demo</a> / <a href="https://gist.github.com/hakre/4959346" rel="nofollow noreferrer">example code as gist</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload