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 Companyclass 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);?> |
array(0) {
}
Program_2:Â
php
<?phpÂ
// Initializing a user-defined functionfunction 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);?> |
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
