PHP | ReflectionClass __toString() Function

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

Syntax:

string ReflectionClass::__toString( void )

Parameters: This function does not accept any parameter.

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

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



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Creating a user-defined function
class Alphabets {
    public function A() {}
    public function B() {}
}
  
// Using ReflectionClass() over the 
// class Alphabets
$class = new ReflectionClass('Alphabets');
  
// Calling the __toString() function
$A = $class->__toString();
  
// Getting the sting representation
var_dump($A);
?>

chevron_right


Output:

string(435) “Class [ <user> class Alphabets ] {
@@ /home/829bc146b7074eafd150c8e57aee5445.php 4-7

– Constants [0] {
}

– Static properties [0] {
}

– Static methods [0] {
}

– Properties [0] {
}

– Methods [2] {
Method [ <user> public method A ] {
@@ /home/829bc146b7074eafd150c8e57aee5445.php 5 – 5
}



Method [ <user> public method B ] {
@@ /home/829bc146b7074eafd150c8e57aee5445.php 6 – 6
}
}
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Using ReflectionClass()
$class = new ReflectionClass('ReflectionClass');
  
// Calling the __toString() function
$A = $class->__toString();
  
// Getting the sting representation
var_dump($A);
?>

chevron_right


Output:

string(6824) “Class [ <internal:Reflection> class ReflectionClass implements Reflector ] {

– Constants [3] {
Constant [ integer IS_IMPLICIT_ABSTRACT ] { 16 }
Constant [ integer IS_EXPLICIT_ABSTRACT ] { 32 }
Constant [ integer IS_FINAL ] { 4 }
}

– Static properties [0] {
}

– Static methods [1] {
Method [ <internal:Reflection, prototype Reflector> static public method export ] {

– Parameters [2] {
Parameter #0 [ <required> $argument ]
Parameter #1 [ <optional> $return ]
}
}
}
. . .

Method [ <internal:Reflection> public method getShortName ] {

– Parameters [0] {
}
}
}
}

Reference: https://www.php.net/manual/en/reflectionclass.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.