← back to the blog

Reflection api example, inheritance and how to inspect private methods

Posted on October 21st, 2018 in PhP by George

Here is an example of PHP Reflection api and class inheritance. This example will cover a lot of things like getDocComment() method of reflection api and insanceof plus other things. In order to make sense of the code read the comments. Caveats: getDocComment will return only comments declared on the global scope of the namesapce/document in case. The comment must start in this form with double asterix /** and end */; 


//first thing bellow is a comment/document declared in global namespace which going
// to be parsed by Reflect method getDocComment();

*Nina got some video from Artem which she would like to share with me.

//define a class

class SundayMorning {

protected $thisMorning = 'cold';

public function howIsCofee($argument = null):string {

$this->thisMorning = $argument;
return $this->thisMorning;


//inherit from SundayMorning

class Nina extends SundayMorning {

private $name = 'Nina';

public function howIsNina() {
// use thisMorning variable by inheritance
return $this->name . ' is ' . $this->thisMorning;

private function getChildrensName () {
return 'Kyrill' . ' Milla';

//here is Reflect api declared
$reflectClass = new ReflectionClass('SundayMorning');
//use of reflect api
//get name of class

//use getDocComment

//new object from nina 
$getIt = new Nina();
var_dump($getIt->howIsNina()); //calling method for test

// new reflection api insance for Nina class
$reflectClassNina = new ReflectionClass('Nina');

//use reflection method getParentClass 

//check if $getIt is instance of Nina class via Reflect api

//inspect private methods
// query the private method using Reflection api
$privateMethod = new ReflectionMethod('Nina','getChildrensName');
//check if private
{ //it it is private make it Accessible
//when is Accessible then invoke it 
var_dump($privateMethod->invoke(new Nina()));