Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check that class method require parameter

Tags:

object

php

class

For example there is Foo class object. Not try-catch expression

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

$foo = new Foo();

if(hasRequiredParams(Object $foo, MethodName 'Bar')){
   do something;
}
like image 704
Oleg Patrushev Avatar asked Jun 12 '26 17:06

Oleg Patrushev


1 Answers

You can do this 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 this, it doesn't mean you should. There may be reasons for needing to inspect the method signature at run-time, but in most cases you shouldn't be basing your application logic on this information.

like image 148
cmbuckley Avatar answered Jun 14 '26 07:06

cmbuckley



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!