反射获取function源码
继承ReflectionFunction,扩展getSource方法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | <?php /* ___ __ _ ____ ____ / | ____/ /____ _ ____ ___ (_)____ / __ \ / __ \ / /| | / __ // __ `// __ `__ \ / // __ \/ /_/ // / / / / ___ |/ /_/ // /_/ // / / / / // // / / /\__, // /_/ / /_/ |_|\__,_/ \__,_//_/ /_/ /_//_//_/ /_//____/ \____/ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 做一款产品 愉悦自己 ****************Powered by Adamin90******************** * @email: 14846869@qq.com * Date: 2018/12/27 * Time: 20:00 * @link: https://www.lixiaopeng.top ******************************************************* */ namespace app\common\logic; class FunctionExtract extends \ReflectionFunction { /** * @return bool|string * @author Adam * Time: 2018/12/27 20:30 * Description:获取function源码 */ public function getSource(){ if (! file_exists ( $this ->getFileName())){ return false; } $startoffset = $this ->getStartLine()-1; $endoffset = $this ->getEndLine()- $this ->getStartLine()+1; // return $this->getFileName(); return join( '' , array_slice (file( $this ->getFileName()), $startoffset , $endoffset )); } public function index(){ echo $this ->getSource( 'id_decode' ); } } |
使用方法:
实例化 FunctionExtract类,构造器传入funtion名称即可
1 2 | $fc = new FunctionExtract( "get_avatar" ); echo $fc ->getSource(); |
输出结果:
1 | function get_avatar($uid) { $avatar = Db::name('user')->where('id', $uid)->value('avatar'); if (!empty($avatar)) { $path = get_file_path($avatar); if($path !== false) { return $path; } } //默认头像地址 return '/static/mobile/images/avatar.jpg'; } |
反射获取Method源码
继承ReflectionMethod,自定义getSource方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class MethodExtract extends \ReflectionMethod { /** * @return bool|string * @author Adam * Time: 2018/12/27 20:35 * Description:利用反射提取Mechod方法 */ public function getSource(){ if (! file_exists ( $this ->getFileName())){ return false; } $startoffset = $this ->getStartLine()-1; $endoffset = $this ->getEndLine()- $this ->getStartLine()+1; return join( '' , array_slice (file( $this ->getFileName()), $startoffset , $endoffset )); } } |
使用:
1 2 | $tm = new MethodExtract( 'app\mobile\controller\Area' , 'citylist' ); echo $tm ->getSource(); |
反射获取class源码
继承ReflectionClass类,自定义getSource方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class ClassExtract extends \ReflectionClass { /** * @return bool|string * @author Adam * Time: 2018/12/27 20:36 * Description:反射获取类源码 */ public function getSource() { if (! file_exists ( $this ->getFileName())) { return false; } $startoffset = $this ->getStartLine() - 1; $endoffset = $this ->getEndLine() - $this ->getStartLine() + 1; return join( '' , array_slice (file( $this ->getFileName()), $startoffset , $endoffset )); } } |
使用:
1 2 | $ce = new ClassExtract( 'app\mobile\controller\Area' ); echo $ce ->getSource(); |
完美
本文为Adamin90原创文章,转载无需和我联系,但请注明来自http://www.lixiaopeng.top