Open In App

PHP | ReflectionGenerator getTrace() Function

Last Updated : 26 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads