如何检查方法是否存在并在PHP中有参数

Esk*_*der 3 php

我想检查一个方法是否存在并且有参数.这是一些片段.

// this only checks if the function exist or not
if(method_exists($controller, 'function_name')) 
{
  //do some stuff
}
Run Code Online (Sandbox Code Playgroud)

但我想做的是

if(method_exists($controller, 'function_name(with_args)'))
{

}
Run Code Online (Sandbox Code Playgroud)

Ale*_*pin 11

您可以使用ReflectionMethod's getParameters获取方法的参数列表.然后,您可以检查该列表的长度或执行参数所需的任何其他操作.

<?php
    class Foo {
        function bar($a, $b, $c) {
            return $a + $b + $c;
        }
    }

    $method = new ReflectionMethod('Foo', 'bar');
    var_dump($method->getParameters());
?>
Run Code Online (Sandbox Code Playgroud)

我不知道你会用它做什么,但我建议不要随便使用反射.你可能会重新考虑你的做事方式.