class MyClass { public function myMethod($param1, $param2 = null) {} } $reflectionMethod = new ReflectionMethod('MyClass', 'myMethod'); echo $reflectionMethod->getNumberOfParameters(); // Output: 2
class MyClass { public function myMethod($param1, $param2) { return $param1 + $param2; } } $reflectionMethod = new ReflectionMethod('MyClass', 'myMethod'); $myClass = new MyClass(); echo $reflectionMethod->invoke($myClass, 2, 3); // Output: 5In this example, we create an instance of the ReflectionMethod class for the `myMethod` method of the `MyClass` class. We also create an instance of the `MyClass` class. We then use the `invoke` method of the ReflectionMethod object to invoke the `myMethod` method dynamically and pass 2 and 3 as its parameters. The result is 5. Package Library: PHP ReflectionMethod class is part of the PHP Standard Library.