Open In App

PHP | ReflectionMethod __toString() Function

Last Updated : 23 Dec, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
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: php
<?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);
?>
Output:
string(94) "Method [ <user> public method GFG ] {
  @@ /home/43d46426def0fce620e2e2cf2bf10e7a.php 6 - 6
}
"
Program 2: php
<?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());
?>
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

Next Article

Similar Reads