Home > Backend Development > PHP Tutorial > PHP uses reflection mechanism to find the location of classes and methods, the location of php_PHP tutorial

PHP uses reflection mechanism to find the location of classes and methods, the location of php_PHP tutorial

WBOY
Release: 2016-07-12 08:53:49
Original
826 people have browsed it

PHP uses the reflection mechanism to find the location of classes and methods. The location of php

The example in this article describes the use of reflection mechanism by PHP 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 on this site: "Summary of PHP ajax skills and applications", "Summary of PHP operations and operator usage", "Summary of PHP network programming skills", "PHP basic syntax" "Introductory Tutorial", "Summary of PHP Office Document Skills (Including Word, Excel, Access, PPT)", "Summary of PHP Date and Time Usage", "Introduction to PHP Object-Oriented Programming", "php String Usage" Summary", "Introduction Tutorial on PHP MySQL Database Operation" and "Summary of Common PHP Database Operation Skills"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1122884.htmlTechArticlePHP uses the reflection mechanism to find the location of classes and methods. The location of php. This article describes the use of reflection mechanism by PHP. Implements the location of search classes and methods. Share it with everyone...
Related labels:
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