Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had similar problem, when checking getClass on reflection parameter when a class was not loaded. I made a wrapper function to get the class name from example netcoder made. Problem was that netcoder code didnt work if it was an array or not an class -> function($test) {} it would return the to string method for the reflection parameter.</p> <p>Below the way how i solved it, im using try catch because my code requires at some point the class. So if i request it the next time, get class works and doesnt throw an exception.</p> <pre><code>/** * Because it could be that reflection parameter -&gt;getClass() will try to load an class that isnt included yet * It could thrown an Exception, the way to find out what the class name is by parsing the reflection parameter * God knows why they didn't add getClassName() on reflection parameter. * @param ReflectionParameter $reflectionParameter * @return string Class Name */ public function ResolveParameterClassName(ReflectionParameter $reflectionParameter) { $className = null; try { // first try it on the normal way if the class is loaded then everything should go ok $className = $reflectionParameter-&gt;getClass()-&gt;name; } // if the class isnt loaded it throws an exception and try to resolve it the ugly way catch (Exception $exception) { if ($reflectionParameter-&gt;isArray()) { return null; } $reflectionString = $reflectionParameter-&gt;__toString(); $searchPattern = '/^Parameter \#' . $reflectionParameter-&gt;getPosition() . ' \[ \&lt;required\&gt; ([A-Za-z]+) \$' . $reflectionParameter-&gt;getName() . ' \]$/'; $matchResult = preg_match($searchPattern, $reflectionString, $matches); if (!$matchResult) { return null; } $className = array_pop($matches); } return $className; } </code></pre>
 

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