一个解析php源码找出类名和所有方法名的函数

function getClassAndMethodFromCode($code = '')
{
    $list = token_get_all($code);
    $count = count($list);
    $data = [];
    $class = '';
    $method = [];
    foreach($list as $k => $v){
        if(is_array($v)){
            $v['name'] = token_name($v[0]);
            $v['content'] = $v[1];
            $v['line'] = $v[2];
            if(in_array($v['name'], ['T_CLASS', 'T_FUNCTION'])){
                $data[] = ['name' => $v['name'], 'index' => $k];
            }
        }
        $list[$k] = $v;
    }
    foreach($data as $v){
        for($i = $v['index'];$i < $count;$i++){
            $item = $list[$i];
            if($item['name'] == 'T_STRING'){
                if($v['name'] == 'T_CLASS'){
                    $class = $item['content'];
                }
                else if($v['name'] == 'T_FUNCTION'){
                    $method[] = $item['content'];
                }
                break;
            }
        }
    }
    return ['class' => $class, 'method' => $method];
}

我用这个来自动扫描源码,在用户组权限分配时自动生成权限

你可能感兴趣的:(php7,php)