PHP | ReflectionMethod getDeclaringClass() Function

The ReflectionMethod::getDeclaringClass() function is an inbuilt function in PHP which is utilized to return the name of the declared class.

Syntax:

 ReflectionClass ReflectionMethod::getDeclaringClass ( void )

Parameters: This function does not accepts any parameter.

Return Value: This function returns the name of the declared class for the reflected method.

Below programs illustrates the ReflectionMethod::getDeclaringClass() function:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declaring a class
class GeeksforGeeks {
      
    // Declaring a protected function
    protected function CSportal($name) {
          
        // Displays output
        return 'Geeks ' . $name;
    }
  
}
  
// Creating an object of ReflectionMethod
$reflectionMethod = new ReflectionMethod(new GeeksforGeeks(), 'CSportal');
  
// Calling getDeclaringClass function
var_dump($reflectionMethod->getDeclaringClass());
?>

chevron_right


Output:

object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(13) "GeeksforGeeks"
}

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Declaring a class
class NidhiSingh {
      
    // Declaring a protected function
    protected function Author($name) {
          
        // Displays output
        return 'Nidhi ' . $name;
    }
  
}
  
// Creating an object of ReflectionMethod
$reflectionMethod = new ReflectionMethod(new NidhiSingh(), 'Author');
  
// Calling getDeclaringClass function
var_dump($reflectionMethod->getDeclaringClass());
?>

chevron_right


Output:

object(ReflectionClass)#2 (1) {
  ["name"]=>
  string(10) "NidhiSingh"
}

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


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.