我有这两个接口:
interface Observer
{
public function notify(Observable $observable, ...$args);
}
interface Observable
{
public static function register(Observer $observer);
public function notifyObservers();
}
这就是我想要实现的:
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是Observable并且EventHandler是Observer,对吗?
那么为什么php认为这些实现与它们各自的接口不兼容呢?
对我所说的“兼容”的简单测试:
class CreateEvent extends Event {}
$createEventObj = new CreateEvent();
if ($createEventObj instanceof Observable) {
echo 'Compatible';
} else {
echo 'Incompatible';
}