ReflectionClass 中的未定义方法错误但方法存在
Undefined method error in ReflectionClass but method exists
我目前正在使用 phpunit
进行一些单元测试。由于存在一些受保护的方法,我不得不使用 Reflection Class
将这些方法的可见性更改为 public
.
初始方法已成功调用,但不知何故卡在特定方法上:
Fatal error: Call to undefined method ReflectionClass::create_schema()in
/vagrant/fuelphp/fuel/app/tests/model/repository/jobpost.php on line 54
但是,通过 get_method()
和 var_dump
转储该方法证明它存在于 class 实例中:
class ReflectionMethod#2317 (2) {
public $name =>
string(13) 'create_schema'
public $class =>
string(34) 'Model_Repository_Feed'
}
然后才是真正的迷惑点,我决定用hasMethod()
看看方法是否存在:
52 echo "If this says 1, class exists: ".$this->_target->hasMethod('create_schema');
53 try {
54 $this->_target->create_schema();
55 }
运行 说的结果,"yes it exists.... but it doesn't":
If this says 1, class exists: 1
Fatal error: Call to undefined method ReflectionClass::create_schema() in /vagrant/fuelphp/fuel/app/tests/model/repository/jobpost.php on line 54
澄清一下这个方法是 public 并且继承自 abstract
父 class:
public function create_schema() {
$this->create_schema_exec(self::$_real_schema_name);
}
如何解决这个问题?
您需要获取包含方法的 class 对象而不是反射对象。
$reflection = new ReflectionClass($className);
$object = $reflection->newInstanceWithoutConstructor();
$object->methodName();
我目前正在使用 phpunit
进行一些单元测试。由于存在一些受保护的方法,我不得不使用 Reflection Class
将这些方法的可见性更改为 public
.
初始方法已成功调用,但不知何故卡在特定方法上:
Fatal error: Call to undefined method ReflectionClass::create_schema()in
/vagrant/fuelphp/fuel/app/tests/model/repository/jobpost.php on line 54
但是,通过 get_method()
和 var_dump
转储该方法证明它存在于 class 实例中:
class ReflectionMethod#2317 (2) {
public $name =>
string(13) 'create_schema'
public $class =>
string(34) 'Model_Repository_Feed'
}
然后才是真正的迷惑点,我决定用hasMethod()
看看方法是否存在:
52 echo "If this says 1, class exists: ".$this->_target->hasMethod('create_schema');
53 try {
54 $this->_target->create_schema();
55 }
运行 说的结果,"yes it exists.... but it doesn't":
If this says 1, class exists: 1
Fatal error: Call to undefined method ReflectionClass::create_schema() in /vagrant/fuelphp/fuel/app/tests/model/repository/jobpost.php on line 54
澄清一下这个方法是 public 并且继承自 abstract
父 class:
public function create_schema() {
$this->create_schema_exec(self::$_real_schema_name);
}
如何解决这个问题?
您需要获取包含方法的 class 对象而不是反射对象。
$reflection = new ReflectionClass($className);
$object = $reflection->newInstanceWithoutConstructor();
$object->methodName();