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


PHP ReflectionMethod export()用法及代碼示例


ReflectionMethod::export()函數是PHP中的一個內置函數,如果將return參數設置為TRUE,則用於以字符串形式返回導出,否則返回NULL。

用法:

string ReflectionMethod::export ( $class, $name, $return )

參數:此函數接受上述和以下所述的三個參數:


  • class:這是初始化類的名稱。
  • name:這是方法的名稱。
  • return:它的值為TRUE或FALSE。使用TRUE將返回導出,而使用FALSE將相反。

返回值:如果將return參數設置為TRUE,則此函數以字符串形式返回導出,否則返回NULL。

以下示例程序旨在說明PHP中的ReflectionMethod::export()函數:
程序_1:

<?php 
  
// Initializing a user-defined class 
class Company { 
  
    protected function GeeksforGeeks($name) { 
        return 'GFG' . $name; 
    } 
} 
  
// Using ReflectionMethod() over the class Company 
$A = new ReflectionMethod(new Company(), 'GeeksforGeeks'); 
  
// Calling the export() function 
$B = $A->export( 'Company', 'GeeksforGeeks', $return = TRUE ); 
  
// Getting the the export as a string if the  
// return parameter has been set to TRUE,  
// otherwise NULL is returned. 
var_dump($B); 
?>

輸出:

string(171) "Method [ <user> protected method GeeksforGeeks ] {
  @@ /home/cd1a603457d3beda20350155354b4363.php 6 - 8

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}
"

程序_2:

<?php 
  
// Initializing some user-defined classes 
class Department1 { 
  
    protected function HR($name) { 
        return 'hr' . $name; 
    } 
} 
class Department2 { 
  
    protected function Coding($name) { 
        return 'coding' . $name; 
    } 
} 
class Department3 { 
  
    protected function Marketing($name) { 
        return 'marketing' . $name; 
    } 
} 
  
// Using ReflectionMethod() over the above classes 
$A = new ReflectionMethod(new Department1(), 'HR'); 
$B = new ReflectionMethod(new Department2(), 'Coding'); 
$C = new ReflectionMethod(new Department3(), 'Marketing'); 
  
// Calling the export() function and  
// Getting the the export as a string if the  
// return parameter has been set to TRUE,  
// otherwise NULL is returned. 
var_dump($A->export( 'Department1', 'HR', $return = TRUE )); 
var_dump($B->export( 'Department2', 'Coding', $return = FALSE )); 
var_dump($C->export( 'Department3', 'Marketing', $return = FALSE )); 
?>

輸出:

string(160) "Method [ <user> protected method HR ] {
  @@ /home/b1fa43e2f382f32d60abd4370db4a4f6.php 6 - 8

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}
"
Method [ <user> protected method Coding ] {
  @@ /home/b1fa43e2f382f32d60abd4370db4a4f6.php 12 - 14

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}

NULL
Method [ <user> protected method Marketing ] {
  @@ /home/b1fa43e2f382f32d60abd4370db4a4f6.php 18 - 20

  - Parameters [1] {
    Parameter #0 [ <required> $name ]
  }
}

NULL

參考: https://www.php.net/manual/en/reflectionmethod.export.php



相關用法


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