Jso*_*owa 2 php reflection return-type
我有以下接口 A 以及类 B 和 C (具有 PHP8.0 静态返回类型):
interface A {
public function getSomething(): self;
}
class B implements A {
public function getSomething(): self
{
return $this;
}
}
class C implements A {
public function getSomething(): static
{
return $this;
}
}
Run Code Online (Sandbox Code Playgroud)
如何获取getSomething()新实例方法的返回类型?ReflectionClass类可以吗?
$classes = [ 'B', 'C'];
$newClass = $classes[array_rand($classes)];
print((new ReflectionClass(new $newClass()))->getMethod('getSomething')->getReturnType());
Run Code Online (Sandbox Code Playgroud)
我的示例返回selforstatic这不是我想要的输出。我想得到一个具体的名称类(A等B)。是否可以明确地没有任何解决方法?
小智 5
很好的解决方案是“ Nette 框架”开发堆栈的一部分,它提供了Nette\Utils\Reflection帮助程序。
更多信息在这里:https ://doc.nette.org/en/utils/reflection#toc-getreturntype
| 归档时间: |
|
| 查看次数: |
1512 次 |
| 最近记录: |