当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。