我有返回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;
}
如何检查每种方法的参数?
我发现,这个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;
}
您仍然可以使用反射类。
$rclass = new ReflectionClass('ClassName');
$method = $rclass->getMethod('methodName');
$method->getNumberOfRequiredParameters;
我认为你应该试试这个:http://php.net/manual/en/reflectionfunctionabstract.getnumberofparameters.php