Wednesday, July 29, 2026
HomeLanguagesPHP | ReflectionGenerator getFunction() Function

PHP | ReflectionGenerator getFunction() Function

The ReflectionGenerator::getFunction() function is an inbuilt function in PHP which is used to return the function name of the specified generator.

Syntax:

ReflectionFunctionAbstract ReflectionGenerator::getFunction ( void )

Parameters: This function does not accept any parameter.

Return Value: This function returns the function name of the specified generator.

Below programs illustrate the ReflectionGenerator::getFunction() 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 getFunction() function
$C = $B->getFunction();
  
// Getting the function name of the
// specified generator 'A'
var_dump($C);
?>


Output:

object(ReflectionMethod)#4 (2) {
  ["name"]=>
  string(3) "GFG"
  ["class"]=>
  string(7) "Company"
}

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 getFunction() function
// and getting the function name of the
// specified generators
var_dump($D->getFunction());
echo "\n";
var_dump($E->getFunction());
echo "\n";
var_dump($F->getFunction());
?>


Output:

object(ReflectionMethod)#10 (2) {
  ["name"]=>
  string(17) "Coding_Department"
  ["class"]=>
  string(11) "Departments"
}

object(ReflectionMethod)#10 (2) {
  ["name"]=>
  string(13) "HR_Department"
  ["class"]=>
  string(11) "Departments"
}

object(ReflectionMethod)#10 (2) {
  ["name"]=>
  string(20) "Marketing_Department"
  ["class"]=>
  string(11) "Departments"
}

Reference: https://www.php.net/manual/en/reflectiongenerator.getfunction.php

RELATED ARTICLES

5 COMMENTS

Most Popular

Dominic
32520 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6903 POSTS0 COMMENTS
Nicole Veronica
12017 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12115 POSTS0 COMMENTS
Shaida Kate Naidoo
7023 POSTS0 COMMENTS
Ted Musemwa
7265 POSTS0 COMMENTS
Thapelo Manthata
6981 POSTS0 COMMENTS
Umr Jansen
6973 POSTS0 COMMENTS