【问题标题】:php ReflectionClass::getMethods does not returns the right number of methodsphp ReflectionClass::getMethods 不返回正确数量的方法
【发布时间】:2011-09-15 20:35:52
【问题描述】:

也许这是一个错误,我不确定。

为什么当我执行这段代码时:

<?php

class testReflection implements Serializable {
    public function serialize() {
    }
    public function unserialize($data) {
    }
    public function getData() {
    }
}

class testReflection2 implements arrayaccess {
    public function offsetSet($offset, $value) {
    }
    public function offsetExists($offset) {
    }
    public function offsetUnset($offset) {
    }
    public function offsetGet($offset) {
    }
    public function getData() {
    }
}

$c = new ReflectionClass('testReflection');

foreach ($c->getMethods() as $method) {
  var_dump($method->name);
}
echo '========================';
$c = new ReflectionClass('testReflection2');

foreach ($c->getMethods() as $method) {
  var_dump($method->name);
}

我得到这个结果:

string(9) "serialize"
string(11) "unserialize"
string(7) "getData"
string(11) "unserialize"
string(9) "serialize"
========================
string(9) "offsetSet"
string(12) "offsetExists"
string(11) "offsetUnset"
string(9) "offsetGet"
string(7) "getData"
string(11) "offsetUnset"
string(9) "offsetSet"
string(9) "offsetGet"
string(12) "offsetExists"

接口中定义的方法出现两次。是bug吗?

【问题讨论】:

    标签: php reflection


    【解决方案1】:

    ReflectionClass::getMethods 在不同的 PHP 版本上不能可靠地工作似乎是一个已知的错误,请参阅user comment

    【讨论】:

      猜你喜欢
      • 2022-08-18
      • 1970-01-01
      • 2017-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-09
      相关资源
      最近更新 更多