[英]Why are not this implementation considered compatible with their interface by PHP?
I have these two interfaces: 我有这两个接口:
interface Observer
{
public function notify(Observable $observable, ...$args);
}
interface Observable
{
public static function register(Observer $observer);
public function notifyObservers();
}
And here is what I am trying to implement: 以下是我要实现的内容:
abstract class EventHandler implements Observer
{
abstract public function notify(Event $event, ...$args);
}
abstract class Event implements Observable
{
private static $handlers = [];
public static function register(EventHandler $handler)
{
self::$handlers []= $handler;
}
public function notifyObservers()
{
//notify loop here...
}
}
Event is an Observable and EventHandler is an Observer , right? Event是Observable , EventHandler是Observer ,对吧?
So why php considers these implementation incompatible with their respective interfaces? 那么为什么php认为这些实现与它们各自的接口不兼容?
A simple test of what I meant by "compatible": 对“兼容”的含义进行简单测试:
class CreateEvent extends Event {}
$createEventObj = new CreateEvent();
if ($createEventObj instanceof Observable) {
echo 'Compatible';
} else {
echo 'Incompatible';
}
This is because of type hinting. 这是因为类型提示。 If your typehint is (Observable $observable) you should use exactly the same typehint in all implementation of this method in all sub-classes.
如果你的typehint是(Observable $ observable),你应该在所有子类的所有实现中使用完全相同的typehint。 Read more here http://php.net/manual/de/language.oop5.typehinting.php .
在这里阅读更多http://php.net/manual/de/language.oop5.typehinting.php 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.