Checking if function exists
<p>I got an problem with checking i a function exists - i know function_exists() and method_exists() but cannot get the functionality i look for</p>

<p>I pass functions through a socket connection like $MM->Player->Play().
After that i eval them and return the result through the socket connection.</p>

<p>I do filter the functions (have to start with $MM, anything else will be rejected),
but i cant get my code to check if the function exists. This is important because
the server will crash if the function does not exists.</p>

<p>Anyone know a solution for this??</p>

<p>Thanks in advance.</p>

<p>the code:</p>

<p><code>$MM = new SDBApplication;</code></p>

<p>In the constructor of SDBApplication:</p>

<p><code>$this-&gt;Player = new SDBPlayer;</code></p>

<p>I tried the following methods to check:
the code:</p>

<p><code>method_exists($MM, "Player::Play");</code>
<code>function_exist("$MM-&gt;Player-&gt;Play);</code></p>

<p>both return false even though the function exists</p>

<p><strong>The solution</strong></p>

<pre><code>$parts = explode('-&gt;', $string);
$numParts = count($parts)-1;
$object = '$MM';
for($i=0; $i &lt; $numParts; $i++){
    $object .= '-&gt;'.$parts[$i];
}
$parts[$numParts+1] = preg_replace('(\\(.*\\))', '', $parts[$numParts]);
eval( '$check = method_exists('.$object.', '.$parts[$numParts+1].');');
</code></pre>

<p>It needs to be eval'd because the input is a string.</p>
