How to check that a class method requires a parameter

For example, there is an object of class Foo. Not a try-catch expression

class Foo {
   function Bar($a){
   }
}

$foo = new Foo();

if(hasRequiredParams(Object $foo, MethodName 'Bar')){
   do something;
}

      

+3
object php class


source to share


1 answer


You can do it with reflection :

class Foo {
    function Bar($a) {
    }
}

$rc = new ReflectionClass('Foo');
$rm = $rc->getMethod('Bar');
var_dump($rm->getNumberOfRequiredParameters()); // 1

      



However, just because you can do it doesn't mean you should. There may be reasons for the need to verify the method signature at runtime, but in most cases you should not base your application logic on this information.

+6


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics