1. 使多个对象都有机会处理请求,从而避免请求的发送者和接收者之间的耦合关系,将这些对象练成一条链,并沿着这条链传递该请求,知道有一个对象处理它为止,<br />中间件就是这个的实现,请求过来,执行中间件,再执行别的,<br />定义好几个接受处理类,类里面定义好下一级处理类,实例化后,传入,绑定责任链,执行首个,会依次执行全部,最终一定要有返回,每个处理都可终止,修改信息,或者跳过<br />责任链对处理进行层层过滤处理
    1. <?php
    2. //定义一个抽象类
    3. abstract class Handler
    4. {
    5. protected $successor;
    6. public function setSuccessor($successor)
    7. {
    8. $this->successor = $successor;
    9. }
    10. abstract public function HandleRequst($request);
    11. }
    12. //实现类
    13. class ConcreteHandler1 extends Handler
    14. {
    15. public function HandleRequst($request)
    16. {
    17. if (!strstr($request, '用户')) {
    18. return '没有用户:' . $request;
    19. }
    20. //进行下一步
    21. return $this->successor->HandleRequst($request);
    22. }
    23. }
    24. class ConcreteHandler2 extends Handler
    25. {
    26. public function HandleRequst($request)
    27. {
    28. if (!strstr($request, '登陆')) {
    29. return '没有登陆:' . $request;
    30. }
    31. //进行下一步
    32. return $this->successor->HandleRequst($request);
    33. }
    34. }
    35. class ConcreteHandler3 extends Handler
    36. {
    37. public function HandleRequst($request)
    38. {
    39. if (strstr($request, '时间')) {
    40. return '登陆成功:' . $request;
    41. } else {
    42. return '过期了:' . $request;
    43. }
    44. }
    45. }
    46. //先绑定责任链关系
    47. $ConcreteHandler1 = new ConcreteHandler1();
    48. $ConcreteHandler2 = new ConcreteHandler2();
    49. $ConcreteHandler3 = new ConcreteHandler3();
    50. $ConcreteHandler1->setSuccessor($ConcreteHandler2);
    51. $ConcreteHandler2->setSuccessor($ConcreteHandler3);
    52. //请求
    53. echo $ConcreteHandler1->HandleRequst("dbwq用户udw登陆gq时间gw");