在php中获取参数的方法数量

时间:2013-07-19 07:16:25

标签: php reflection methods

我有代码返回JSON-RPC的服务描述对象,我遇到了类似的问题:PHP: Get number of parameters a function requires但我没有函数,而是函数。

function service_description($object) {
    $class = get_class($object);
    $methods = get_class_methods($class);
    $service = array("sdversion" => "1.0",
                     "name" => "DemoService",
                     "address" => $_SERVER['PHP_SELF'],
                     "id" => "urn:md5:" . md5($_SERVER['PHP_SELF']));
    foreach ($methods as $method) {
        $service['procs'][] = array(
            "name" => $method,
            "params" => ?????
        );
    }
    return $service;
}

如何检查每种方法的参数?

3 个答案:

答案 0 :(得分:4)

我发现,有ReflectionMethod的课程。

foreach ($methods as $method_name) {
    $proc = array("name" => $method_name);
    $method = new ReflectionMethod($class, $method_name);
    $params = array();
    foreach ($method->getParameters() as $param) {
        $params[] = $param->name;
    }
    $proc['params'] = $params;
}

答案 1 :(得分:1)

您仍然可以使用反射类。

$rclass = new ReflectionClass('ClassName');
$method = $rclass->getMethod('methodName');
$method->getNumberOfRequiredParameters;

答案 2 :(得分:0)