class Calculator { public function add($a, $b) { return $a + $b; } } $calculator = new Calculator(); $method = new ReflectionMethod('Calculator', 'add'); $result = $method->invokeArgs($calculator, array(2, 3)); // $result = 5
class Person { public function sayHello($name) { echo "Hello, " . $name . "!"; } } $person = new Person(); $method = new ReflectionMethod('Person', 'sayHello'); $args = array('John'); $method->invokeArgs($person, $args); // Output: Hello, John!In this example, we create a Person class with a sayHello method that accepts one argument. We create an instance of the Person class, get a reference to the sayHello method using ReflectionMethod, and invoke the method with the argument using invokeArgs. Package/library: The PHP Reflection API is part of the PHP core language, so there is no external package or library required.