Home > Article > Backend Development > PHP uses reflection mechanism to find the location of classes and methods_php tips
The example in this article describes how PHP uses the reflection mechanism to find the location of classes and methods. Share it with everyone for your reference, the details are as follows:
//参数1是类名,参数2是方法名 $func = new ReflectionMethod('UnifiedOrder_pub', 'getPrepayId'); //从第几行开始 $start = $func->getStartLine() - 1; //从第几行结束 $end = $func->getEndLine() - 1; //获取路径地址 $filename = $func->getFileName();
The following is a more comprehensive excerpt of the sample code
<?php function a() { } class b { public function f() { } } function function_dump($funcname) { try { if(is_array($funcname)) { $func = new ReflectionMethod($funcname[0], $funcname[1]); $funcname = $funcname[1]; } else { //这个应该是当只有一个参数的时候就看做是本类的发放吧,大概,自行百度 $func = new ReflectionFunction($funcname); } } catch (ReflectionException $e) { echo $e->getMessage(); return; } $start = $func->getStartLine() - 1; $end = $func->getEndLine() - 1; $filename = $func->getFileName(); echo "function $funcname defined by $filename($start - $end)\n"; } function_dump('a'); function_dump(array('b', 'f')); $b = new b(); function_dump(array($b, 'f')); ?>
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP ajax skills and applications", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "Introduction to PHP basic syntax tutorial", "Summary of PHP office document skills (including word, excel, access, ppt)", "php date and time usage summary", "php object-oriented programming introductory tutorial", "php string (string) usage summary" , "php mysql database operation introductory tutorial" and "php common database operation skills summary"
I hope this article will be helpful to everyone in PHP programming.