1

我有返回服务描述对象的代码,JSON-RPC然后我有类似的问题:PHP:获取函数所需的参数数量,但我有一个方法而不是函数。

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;
}

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

4

3 回答 3

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;
}
于 2013-07-19T07:22:41.693 回答
1

您仍然可以使用反射类。

$rclass = new ReflectionClass('ClassName');
$method = $rclass->getMethod('methodName');
$method->getNumberOfRequiredParameters;
于 2013-07-19T07:25:55.897 回答
0

我想你应该试试这个: http: //php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php

于 2013-07-19T07:25:00.500 回答