Heim > Artikel > Backend-Entwicklung > 解析php中反射的应用_PHP教程
一 反射的使用:
<?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>