PHP obtains class source code through reflection

Source: Internet
Author: User
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 ";}}"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.