Home > php教程 > php手册 > PHP获取指定函数定义在哪个文件中以及其所在的行号实例

PHP获取指定函数定义在哪个文件中以及其所在的行号实例

WBOY
Release: 2016-06-06 20:22:49
Original
1991 people have browsed it

这篇文章主要介绍了PHP获取指定函数定义在哪个文件中以及其所在的行号实例,需要的朋友可以参考下

当调试开源的代码时,希望查看某个函数的定义,那么就需要定位其位置。在 zend studio 这样的 IDE 中自是可以自动提示到,但当没有安装这样的开发工具时,我们可以怎么办呢?参考如下一段代码,,或许就包含你所需的。

复制代码 代码如下:


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'));
?>

Related labels:
php
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template