1 <? PHP 2 function getsource (reflectionclass $ ref) {3 $ Path = $ ref-> getfilename (); # Get the script file name 4 $ file = file ($ PATH ); # The file () method gets the file content and saves the content in an array. Each element of the array saves a row of content 5 $ start = $ ref-> getstartline (); # obtain the row number of the first line of the class in the script 6 $ end = $ ref-> getendline (); # Get the row number of the class in the last line of the script 7 $ source = implode (array_slice ($ file, $ start-1, $ end-$ start + 1 )); # Assembly class source code 8 9 var_dump ($ source); 10} 11 12 class person {13 public $ age; 14 private $ Name; 15 16 function say () {17 echo "yes"; 18} 19} 20 21 $ ref = new reflectionclass ("person"); 22 getsource ($ ref ); 23?>
Output
String (145) "class person {public $ age; private $ name; function say () {echo" yes ";}}"