PHP 反射獲取函數體

PHP 通過反射獲取函數體

<?php
function hello()
{
    var_dump("hello");
}

function get_function_define($closure)
{
    try {
        $func = new ReflectionFunction($closure);
    } catch (ReflectionException $e) {
        echo $e->getMessage();
        return;
    }

    $start = $func->getStartLine() - 1;

    $end = $func->getEndLine() - 1;

    $filename = $func->getFileName();

    echo implode("", array_slice(file($filename), $start, $end - $start + 1));
}

get_function_define("hello");

這樣就能拿到函數體了。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章