Skip to content
Related Articles

Related Articles

PHP | ReflectionFunction __toString() Function
  • Last Updated : 19 Dec, 2019

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

Syntax:

string ReflectionFunction::__toString( void )

Parameters: This function does not accept any parameters.

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

Below programs illustrate the ReflectionFunction::__toString() function in PHP:



Program 1:




<?php
  
// Initializing a user-defined function
function Company($Company_Name, $Role)
{
    return sprintf("%s %s\r\n", $Company_Name, $Role);
}
  
// Using ReflectionFunction() over the specified
// function company
$function = new ReflectionFunction('company');
  
// Calling the __toString() function
$A = $function->__toString();
  
// Getting the string representation of the
// above specified function
echo $A;
?>

Output:

Function [ <user> function Company ] {
  @@ /home/a39c30763ecfc7f257d02e44de746b22.php 4 - 7

  - Parameters [2] {
    Parameter #0 [ <required> $Company_Name ]
    Parameter #1 [ <required> $Role ]
  }
}

Program 2:




<?php
  
// Initializing some user-defined functions
function Trial1($First_Args, $Second_Args)
{
    return sprintf("%s %s\r\n", $First_Args, $Second_Args);
}
  
function Trial2($First_Args, $Second_Args)
{
    return sprintf("%s %s\r\n", $First_Args, $Second_Args);
}
  
// Using ReflectionFunction() over the above
// specified functions 
$function = new ReflectionFunction('Trial1');
$function = new ReflectionFunction('Trial2');
  
// Calling the __toString() function and
// Getting string representation of the
// above specified funtion
echo $function->__toString();
echo $function->__toString();
?>

Output:

Function [ <user> function Trial2 ] {
  @@ /home/09f598906ca0bfcad4613b5dea41c27b.php 9 - 12

  - Parameters [2] {
    Parameter #0 [ <required> $First_Args ]
    Parameter #1 [ <required> $Second_Args ]
  }
}
Function [ <user> function Trial2 ] {
  @@ /home/09f598906ca0bfcad4613b5dea41c27b.php 9 - 12

  - Parameters [2] {
    Parameter #0 [ <required> $First_Args ]
    Parameter #1 [ <required> $Second_Args ]
  }
}

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :