Home > Article > Backend Development > Analyze the application of reflection in php_PHP tutorial
一 反射的使用:
<?php<BR>class ReflectionUtil{<BR> static function getClassSource(ReflectionClass $class){<BR> $path=$class->getFileName();<br> $lines=@file($path);<br> $from=$class->getStartLine();<br> $to=$class->getEndLine();<br> $len=$to-$from+1;<br> return implode(array_slice($lines,$from-1,$len));<br> }<br>}<br>$classname="Person";<br>$path="../practice/{$classname}.php";<br>if(!file_exists($path)){<br> throw new Exception("No such file as {$path}");<br>}<br>require_once($path);<br>if(!class_exists($classname)){<br> throw new Exception("No such class as {$classname}");<br>}<br>print ReflectionUtil::getClassSource(new ReflectionClass('Person'));<br>?><br>