如何检查变量是if块中的对象还是字符串?似乎无法调用{% if ... %}块内的函数.而另一种{{ if() }}语法似乎只适用于内联条件.
我现在解决它来测试变量是一个对象时应该存在的一些对象属性,但应该有一个更好的解决方案.像一个isObject或isString功能
我实现了一个ListenerAggregateInterface用于侦听dispatch(MvcEvent::EVENT_DISPATCH)事件的方法.
public function attach(EventManagerInterface $events) {
$this->listeners[] = $events->attach(
MvcEvent::EVENT_DISPATCH,
array($this, 'shortCircuit'),
101
);
$this->listeners[] = $events->attach(
MvcEvent::EVENT_DISPATCH,
array($this, 'listener1'),
33
);
$this->listeners[] = $events->attach(
MvcEvent::EVENT_DISPATCH,
array($this, 'listener2'),
33
);
}
Run Code Online (Sandbox Code Playgroud)
如果某些条件shortCiruit是true其余的听众应该被跳过.因此,我打电话ListenerAggregateInterface::detach删除所有听众.
public function shortCircuit(MvcEvent $e) {
if(condition) {
$this->detach($e->getApplication()->getEventManager());
}
}
Run Code Online (Sandbox Code Playgroud)
我原以为他们现在不再被执行,但事实并非如此.