您好,欢迎访问一九零五行业门户网

windFramework_拦截链

$method(); } //取到拦截链中的下一个拦截器 $handler = $this->interceptorchain->gethandler(); //递归循环,把所有拦截链中的拦截器都循环一遍,并挨个执行这个方法 if (null !== $handler){ $handler->handle($method); } return; } /** * 设置拦截链对象(用以传递控制到下一个拦截器) * * @param interceptorchain $interceptorchain */ public function sethandlerinterceptorchain($interceptorchain) { $this->interceptorchain = $interceptorchain; } }/** * 拦截链 */class interceptorchain{ /** * 拦截器 * @var array */ protected $_interceptors = array('_na' => null); /** * 得到下一个拦截器 * @return interceptorchain|null|interceptor */ public function gethandler() { if (count($this->_interceptors) _interceptors); //如果没有下一个指针,则指针指向第一个元素并返回null if ($handler === false) { reset($this->_interceptors); return null; } if (method_exists($handler, 'handle')) { //设计拦截器基类中的拦截链 $handler->sethandlerinterceptorchain($this); //返回拦截器对象 return $handler; } return $this->gethandler(); } /** * 往拦截链中添加拦截器 * @param interceptor $interceptors */ public function addinterceptors($interceptors) { if (is_array($interceptors)) $this->_interceptors = array_merge($this->_interceptors, $interceptors); else $this->_interceptors[] = $interceptors; } /** * 重置拦截链初始化信息 * @return boolean */ public function reset() { $this->_interceptors = array('_na' => null); return true; }}//拦截器一class interceptorone extends interceptor{ public function interceptmethod(){ echo 执行拦截器1中的拦截方法; }}//拦截器二class interceptortwo extends interceptor{ public function interceptmethod(){ echo 执行拦截器2中的拦截方法; }}//先弄一个拦截链$interceptorchain = new interceptorchain();//再弄两个拦截器$interceptorone = new interceptorone();$interceptortwo = new interceptortwo();//再把拦截器 放到拦截链里面$interceptorchain->addinterceptors($interceptorone);$interceptorchain->addinterceptors($interceptortwo);//运行拦截链$interceptorchain->gethandler()->handle('interceptmethod');// 执行拦截器1中的拦截方法// 执行拦截器2中的拦截方法
遗留问题:
1,为什么拦截链中默认有一个'_na'的键位,是做什么的,默认为空数组不就好了?
以上就介绍了windframework_拦截链,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。
其它类似信息

推荐信息