Tuesday, September 16, 2025
HomeLanguagesPHP | ReflectionGenerator getTrace() Function

PHP | ReflectionGenerator getTrace() Function

The ReflectionGenerator::getTrace() function is an inbuilt function in PHP which is used to return the trace of the specified currently executing generator. Syntax:

array ReflectionGenerator::getTrace ( void )

Parameters: This function does not accept any parameter. Return Value: This function returns the trace of the specified currently executing generator. Below programs illustrate the ReflectionGenerator::getTrace() function in PHP: Program_1: 

php




<?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 getTrace() function
$C = $B->getTrace();
 
// Getting the trace of the specified
// executing generator 'A'
var_dump($C);
?>


Output:

array(0) {
}

Program_2: 

php




<?php
 
// Initializing a user-defined function
function Department1() {
    yield 1;
}
 
function Department2()
{
    yield from Department1();
}
 
function Department3()
{
    yield from Department2();
}
 
// Creating a generator
$A = Department3();
 
// Starting the generator
$A->valid();
 
// Using ReflectionGenerator() over the
// above generator 'A'
$A = new ReflectionGenerator($A);
 
// Calling the getTrace() function
$B = $A->getTrace();
 
// Getting the trace of the
// executing generator 'A'
var_dump($B);
?>


Output:

array(2) {
  [0]=>
  array(4) {
    ["file"]=>
    string(42) "/home/5ae62f6794b195f5dfeff893639bead9.php"
    ["line"]=>
    int(10)
    ["function"]=>
    string(11) "Department1"
    ["args"]=>
    array(0) {
    }
  }
  [1]=>
  array(4) {
    ["file"]=>
    string(42) "/home/5ae62f6794b195f5dfeff893639bead9.php"
    ["line"]=>
    int(15)
    ["function"]=>
    string(11) "Department2"
    ["args"]=>
    array(0) {
    }
  }
}

Reference: https://www.php.net/manual/en/reflectiongenerator.gettrace.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
32293 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6656 POSTS0 COMMENTS
Nicole Veronica
11826 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11892 POSTS0 COMMENTS
Shaida Kate Naidoo
6773 POSTS0 COMMENTS
Ted Musemwa
7044 POSTS0 COMMENTS
Thapelo Manthata
6729 POSTS0 COMMENTS
Umr Jansen
6739 POSTS0 COMMENTS