首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

ReflectionClass::getMethods

(PHP 5, PHP 7)

ReflectionClass::getMethods - 获取一组方法

描述

代码语言:javascript
复制
public array ReflectionClass::getMethods ([ int $filter ] )

获取该类的一组方法。

参数

filter

筛选结果以仅包含具有某些属性的方法。默认为不过滤。

任何按位析取ReflectionMethod::IS_STATICReflectionMethod::IS_PUBLICReflectionMethod::IS_PROTECTEDReflectionMethod::IS_PRIVATEReflectionMethod::IS_ABSTRACTReflectionMethod::IS_FINAL,让所有方法的任何给定的属性将被退回。

注意:请注意,其他按位操作(例如?)无法按预期工作。换句话说,例如,不可能检索所有非静态方法。

返回值

反映每种方法的ReflectionMethod对象数组。

示例

Example#1 ReflectionClass::getMethods()的基本用法

代码语言:javascript
复制
<?php
class?Apple?{
????public?function?firstMethod()?{?}
????final?protected?function?secondMethod()?{?}
????private?static?function?thirdMethod()?{?}
}

$class?=?new?ReflectionClass('Apple');
$methods?=?$class->getMethods();
var_dump($methods);
?>

上面的例子将输出:

代码语言:javascript
复制
array(3) {
  [0]=>
  &object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(11) "firstMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(12) "secondMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [2]=>
  &object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(11) "thirdMethod"
    ["class"]=>
    string(5) "Apple"
  }
}

Example#2从ReflectionClass::getMethods()过滤结果

代码语言:javascript
复制
<?php
class?Apple?{
????public?function?firstMethod()?{?}
????final?protected?function?secondMethod()?{?}
????private?static?function?thirdMethod()?{?}
}

$class?=?new?ReflectionClass('Apple');
$methods?=?$class->getMethods(ReflectionMethod::IS_STATIC?|?ReflectionMethod::IS_FINAL);
var_dump($methods);
?>

上面的例子将输出:

代码语言:javascript
复制
array(2) {
  [0]=>
  &object(ReflectionMethod)#2 (2) {
    ["name"]=>
    string(12) "secondMethod"
    ["class"]=>
    string(5) "Apple"
  }
  [1]=>
  &object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(11) "thirdMethod"
    ["class"]=>
    string(5) "Apple"
  }
}

另请参阅

  • ReflectionClass::getMethod() - 获取类方法的ReflectionMethod。
  • get_class_methods() - 获取类方法的名称

← ReflectionClass::getMethod

ReflectionClass::getModifiers →

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com