使用 PHP 魔术方法实现的带注释的方法不被 "hasMethod" 识别。
<?php
class SomeClass
{
public function __call($name, $arguments)
{
echo "this is magic method: $name.\n";
}
public function codedMethod()
{
echo "this is coded method.\n";
}
}
$obj = new \SomeClass();
$obj->codedMethod();
$obj->annotatedMethod();
$ref = new ReflectionClass(\SomeClass::class);
echo "SomeClass has 'codedMethod': " . json_encode($ref->hasMethod('codedMethod')) . ".\n";
echo "SomeClass has 'annotatedMethod': " . json_encode($ref->hasMethod('annotatedMethod')) . ".\n";
?>
输出
this is coded method.
this is magic method: annotatedMethod.
SomeClass has 'codedMethod': true.
SomeClass has 'annotatedMethod': false.