Friday, October 10, 2025
HomeLanguagesPHP | ReflectionGenerator getThis() Function

PHP | ReflectionGenerator getThis() Function

The ReflectionGenerator::getThis() function is an inbuilt function in PHP which is used to return the $this value of the specified generator.

Syntax:

object ReflectionGenerator::getThis ( void )

Parameters: This function does not accept any parameter.

Return Value: This function returns the $this value of the specified generator.

Below programs illustrate the ReflectionGenerator::getThis() function in PHP:
Program_1:




<?php
  
// Initializing a user-defined class Company
class Company
{
    public function GFG()
    {
        yield 0;
    }
}
  
// Creating a generator 'A' on the above
// class Company
$A = (new Company)->GFG();
  
// Using ReflectionGenerator over the 
// above generator 'A'
$B = new ReflectionGenerator($A);
  
// Calling the getThis() function
$C = $B->getThis();
  
// Getting the $this value of the
// specified generator 'A'
var_dump($C);
?>


Output:

object(Company)#1 (0) {
}

Program_2:




<?php
  
// Initializing a user-defined class Departments
class Departments
{
    public function Coding_Department()
    {
        yield 0;
    }
    public function HR_Department()
    {
        yield 1;
    }
    public function Marketing_Department()
    {
        yield 2;
    }
}
  
// Creating some generators on the above
// class Departments
$A = (new Departments)->Coding_Department();
$B = (new Departments)->HR_Department();
$C = (new Departments)->Marketing_Department();
  
// Using ReflectionGenerator over the 
// above generators
$D = new ReflectionGenerator($A);
$E = new ReflectionGenerator($B);
$F = new ReflectionGenerator($C);
  
// Calling the getThis() function
// and getting the $this value of the
// specified generators
var_dump($D->getThis());
echo "\n";
var_dump($E->getThis());
echo "\n";
var_dump($F->getThis());
?>


Output:

object(Departments)#1 (0) {
}

object(Departments)#3 (0) {
}

object(Departments)#5 (0) {
}

Reference: https://www.php.net/manual/en/reflectiongenerator.getthis.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
32350 POSTS0 COMMENTS
Milvus
87 POSTS0 COMMENTS
Nango Kala
6718 POSTS0 COMMENTS
Nicole Veronica
11880 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11941 POSTS0 COMMENTS
Shaida Kate Naidoo
6838 POSTS0 COMMENTS
Ted Musemwa
7101 POSTS0 COMMENTS
Thapelo Manthata
6794 POSTS0 COMMENTS
Umr Jansen
6794 POSTS0 COMMENTS