php - PHP interface in extended class not recognized as instance
问题描述
I have an abstract class A
which is extended by class B
which implements the interface I
.
abstract class A {
public function test(){
return $this->getX();
}
abstract protected function getX();
}
class B extends A implements I {
public function Test() {
$x = this->getX();
if (!$x instanceof I) {
throw new RuntimeException("not an instance of I");
}
}
public function getX() {
$aCoordinates = array('x' => 1, 'y' => 4, 'z' => 5);
return $aCoordinates;
}
}
interface I {}
The RuntimeException
is always thrown, despite $x
is an instance of I
. Anyone an idea why this happens?
getX()
returns an array. getX()
is just an example for a function returning an array.
解决方案
该getX()
方法不返回任何数据所以值$x
是null;
有一个I接口的对象实例,你需要把它放在return $this
方法中getX()
。
推荐阅读
- python - 遍历字典列表使我的功能变得异常缓慢
- html - 通过过渡更改图像不透明度
- javascript - 如何等待回调完成从 ES6 代理返回
- javascript - 如何使用Vue路由器更改轮播幻灯片?
- sql - 在 Oracle DB Developer VM 中启动时 SQL Developer 崩溃
- c# - 未找到 C# .NET UserNotificationListener 元素
- time - Raspberry Pi Pico - 纳秒计时器
- gluon - Gluon Native DRM javaFX 错误,无法启动基于 DRM 的 JavaFX Native 应用程序
- python - 基于列值 0 或 1 的行的平均值
- .net - 如何在 .NET Core 5 上将 example.com 重定向到 example.com/espanol