PHP uses reflection mechanism to find the location of classes and methods_php tips

WBOY
Release: 2016-05-16 19:54:04
Original
1014 people have browsed it

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();

Copy after login

The following is a more comprehensive excerpt of the sample code

<&#63;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'));
&#63;>

Copy after login

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.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!