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


PHP ReflectionClass getMethods()用法及代碼示例


ReflectionClass::getMethods()函數是PHP中的內置函數,用於返回指定方法的數組。

用法:

array ReflectionClass::getMethods( int $filter )

參數:該函數接受單個參數過濾器,該過濾器用於刪除某些方法。


返回值:此函數返回指定方法的數組。

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

示例1:

<?php 
  
// Initialising a user-defined Class 
class Departments { 
    public function CSE() { } 
    final protected function ECE() { } 
    private static function EE() { } 
    static function IT() { } 
    private function Mechnical() { } 
} 
  
// Using ReflectionClass() over the 
// user-defined class Departments 
$class = new ReflectionClass('Departments'); 
  
// Calling the getMethods() function 
$methods = $class->getMethods(); 
  
// Getting an array of specified methods 
var_dump($methods); 
?>

輸出:

array(5) {
  [0]=>
  object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(3) "CSE"
    ["class"]=>
    string(11) "Departments"
  }
  [1]=>
  object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(3) "ECE"
    ["class"]=>
    string(11) "Departments"
  }
  [2]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(2) "EE"
    ["class"]=>
    string(11) "Departments"
  }
  [3]=>
  object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(2) "IT"
    ["class"]=>
    string(11) "Departments"
  }
  [4]=>
  object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(9) "Mechnical"
    ["class"]=>
    string(11) "Departments"
  }
}

示例2:

<?php 
  
// Initialising a user-defined Class 
class Departments { 
    public function CSE() { } 
    final protected function ECE() { } 
    private static function EE() { } 
    static function IT() { } 
    private function Mechnical() { } 
} 
  
// Using ReflectionClass() over the 
// user-defined class Departments 
$class = new ReflectionClass('Departments'); 
  
// Calling the getMethods() function 
$methods = $class->getMethods(ReflectionMethod::IS_STATIC); 
  
// Getting an array of specified methods 
var_dump($methods); 
?>

輸出:

array(2) {
  [0]=>
  object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(2) "EE"
    ["class"]=>
    string(11) "Departments"
  }
  [1]=>
  object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(2) "IT"
    ["class"]=>
    string(11) "Departments"
  }
}

參考: https://www.php.net/manual/en/reflectionclass.getmethods.php



相關用法


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