Home  >  Article  >  Backend Development  >  PHP uses the reflection mechanism to find the location of classes and methods. PHP autoload mechanism. PHP lock mechanism. PHP plug-in machine.

PHP uses the reflection mechanism to find the location of classes and methods. PHP autoload mechanism. PHP lock mechanism. PHP plug-in machine.

WBOY
WBOYOriginal
2016-07-29 08:52:291009browse

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 topic of this site: "PHP+ajax skills and Application Summary", "PHP Operations and Operator Usage Summary", "PHP Network Programming Skills Summary", "PHP Basic Grammar Introductory Tutorial", "php Office Document Skills Summary (including word, excel, access, ppt)", " Summary of PHP Date and Time Usage", "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP+MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

Hope What this article describes will be helpful to everyone in PHP programming.

The above introduces how PHP uses the reflection mechanism to find the location of classes and methods, including PHP and mechanism aspects. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
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