PHP | ReflectionMethod __toString() Function

The ReflectionMethod::__toString() function is an inbuilt function in PHP which is used to return the string representation of the specified method object.

Syntax:

string ReflectionMethod::__toString ( void )

Parameters: This function does not accept any parameter.

Return Value: This function returns the string representation of the specified method object.

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



filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Initializing a user-defined class
class Company {
  
     function GFG() {}
}
  
// Using ReflectionMethod() over the class Company
$A = new ReflectionMethod('Company', 'GFG');
  
// Calling the __toString() function
$B = $A->__toString();
  
// Getting the string representation of 
// the specified method object 'GFG'
var_dump($B);
?>

chevron_right


Output:

string(94) "Method [ <user> public method GFG ] {
  @@ /home/43d46426def0fce620e2e2cf2bf10e7a.php 6 - 6
}
"

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
   
// Initializing some user-defined classes
class Department1 {
   
    private function hr($name) {
        return 'HR' . $name;
    }
}
class Department2 {
   
    static function Coding() {}
}
  
class Department3 {
   
     protected function marketing(){}
}
  
// Using ReflectionMethod() over the above classes
$A = new ReflectionMethod('Department1', 'hR');
$B = new ReflectionMethod('Department2', 'Coding');
$C = new ReflectionMethod('Department3', 'marketing');
   
// Calling the __toString() function and 
// getting the string representation of the
// above method objects
var_dump($A->__toString());
var_dump($B->__toString());
var_dump($C->__toString());
?>

chevron_right


Output:

string(158) "Method [ <user> private method hr ] {
  @@ /home/2aa9608c9056b85288d53e96d9de3310.php 6 - 8

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}
"
string(106) "Method [ <user> static public method Coding ] {
  @@ /home/2aa9608c9056b85288d53e96d9de3310.php 12 - 12
}
"
string(105) "Method [ <user> protected method marketing ] {
  @@ /home/2aa9608c9056b85288d53e96d9de3310.php 17 - 17
}
"

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




My Personal Notes arrow_drop_up

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.