PHP 通过反射获取某个类的方法

| 选择喜欢的代码风格  

ReflectionClass 类报告了一个类的有关信息,比如通过调用 ReflectionClass ,来获取 protobuf 3 中支持的方法列表:

<?php
$class = new ReflectionClass('\Google\Protobuf\Internal\Message');
$methods = $class->getMethods();
print_e($methods);

返回结果:

array(14) {
  [0]=>
  object(ReflectionMethod)#3 (2) {
    ["name"]=>
    string(5) "clear"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [1]=>
  object(ReflectionMethod)#4 (2) {
    ["name"]=>
    string(20) "discardUnknownFields"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [2]=>
  object(ReflectionMethod)#5 (2) {
    ["name"]=>
    string(17) "serializeToString"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [3]=>
  object(ReflectionMethod)#6 (2) {
    ["name"]=>
    string(15) "mergeFromString"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [4]=>
  object(ReflectionMethod)#7 (2) {
    ["name"]=>
    string(21) "serializeToJsonString"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [5]=>
  object(ReflectionMethod)#8 (2) {
    ["name"]=>
    string(19) "mergeFromJsonString"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [6]=>
  object(ReflectionMethod)#9 (2) {
    ["name"]=>
    string(9) "mergeFrom"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [7]=>
  object(ReflectionMethod)#10 (2) {
    ["name"]=>
    string(16) "readWrapperValue"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [8]=>
  object(ReflectionMethod)#11 (2) {
    ["name"]=>
    string(17) "writeWrapperValue"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [9]=>
  object(ReflectionMethod)#12 (2) {
    ["name"]=>
    string(8) "hasOneof"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [10]=>
  object(ReflectionMethod)#13 (2) {
    ["name"]=>
    string(9) "readOneof"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [11]=>
  object(ReflectionMethod)#14 (2) {
    ["name"]=>
    string(10) "writeOneof"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [12]=>
  object(ReflectionMethod)#15 (2) {
    ["name"]=>
    string(10) "whichOneof"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
  [13]=>
  object(ReflectionMethod)#16 (2) {
    ["name"]=>
    string(11) "__construct"
    ["class"]=>
    string(32) "Google\Protobuf\Internal\Message"
  }
}

php ReflectionClass 扩展阅读:




发表评论