當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


PHP ReflectionGenerator getExecutingGenerator()用法及代碼示例


ReflectionGenerator::getExecutingGenerator()函數是PHP中的內置函數,用於返回當前正在執行的Generator對象。

用法:

Generator ReflectionGenerator::getExecutingGenerator( void )

參數:該函數不接受任何參數。


返回值:此函數返回當前正在執行的Generator對象。

以下示例程序旨在說明PHP中的ReflectionGenerator::getExecutingGenerator()函數:

示例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 getExecutingGenerator() function 
$C = $B->getExecutingGenerator(); 
  
// Getting the currently executing 
// Generator object 
var_dump($C->current()); 
?>

輸出:

int(0)

示例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 getExecutingGenerator() function 
// and getting the currently executing 
// Generator object 
var_dump($D->getExecutingGenerator()->current()); 
var_dump($E->getExecutingGenerator()->current()); 
var_dump($F->getExecutingGenerator()->current()); 
?>

輸出:

int(0)
int(1)
int(2)

參考: https://www.php.net/manual/en/reflectiongenerator.getexecutinggenerator.php



相關用法


注:本文由純淨天空篩選整理自Kanchan_Ray大神的英文原創作品 PHP | ReflectionGenerator getExecutingGenerator() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。