Tuesday, October 7, 2025
HomeLanguagesPHP | ReflectionFunction __toString() Function

PHP | ReflectionFunction __toString() Function

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




<?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




<?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 function
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
 

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS