PHP | ReflectionMethod invokeArgs() Function

The ReflectionMethod::invokeArgs() function is an inbuilt function in PHP which is used to invoke the specified reflected method and returns the result of the mothod.

Syntax:

mixed ReflectionMethod::invokeArgs ( $object, $parameter )

Parameters: This function accepts two parameters as mentined above and described below:

  • object: This is the initialized class object.
  • parameter: This is the array of zero or more parameters to be passed to the method.

Return Value: This function returns the result of the invoked method.

Below programs illustrate the ReflectionMethod::invokeArgs() function in PHP:
Program 1:



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Initializing a user-defined class
class Company {
  
    public function GFG($name) {
        return 'GeeksforGeeks' . $name;
    }
}
  
// Using ReflectionMethod() over the class Company
$A = new ReflectionMethod('Company', 'GFG');
  
// Calling the invokeArgs() function
$B = $A->invokeArgs(new Company(), 
      array(' is a Computer Science Portal.'));
  
// Getting the result of the invoked method.
echo $B;
?>

chevron_right


Output:

GeeksforGeeks is a Computer Science Portal.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Initializing some user-defined classes
class Department1 {
   
    public function hr($name) {
        return 'HR' . $name;
    }
}
class Department2 {
   
    public function coding($name) {
        return 'Coding' . $name;
    }
}
class Department3 {
   
    public function marketing($name) {
        return 'Marketing' . $name;
    }
}
  
// Using ReflectionMethod() over the above classes
$A = new ReflectionMethod('Department1', 'hR');
$B = new ReflectionMethod('Department2', 'coding');
$C = new ReflectionMethod('Department3', 'marketing');
   
// Calling the invokeArgs() function and 
// getting the result of the invoked method.
echo $A->invokeArgs(new Department1(), 
                      array(' is a Department.'));
echo "\n";
echo $B->invokeArgs(new Department2(), 
                 array(' is also a Department.'));
echo "\n";
echo $C->invokeArgs(new Department3(), 
                                  array(' too.'));
?>

chevron_right


Output:

HR is a Department.
Coding is also a Department.
Marketing too.

Reference: https://www.php.net/manual/en/reflectionmethod.invokeargs.php




My Personal Notes arrow_drop_up

Image
Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.