Пример #1
0
    {
        if (count($param) == 2) {
            var_dump(__CLASS__);
            var_dump($param);
        } else {
            $class = $this->handleList[$this->handleCurrent++];
            (new $class())->action($param);
        }
    }
}
class handle3 extends handle
{
    public function action(array $param)
    {
        if (count($param) == 3) {
            var_dump(__CLASS__);
            var_dump($param);
        } else {
            $class = $this->handleList[$this->handleCurrent++];
            (new $class())->action($param);
        }
    }
}
//责任链的第一个处理handle一定会实例化的
//建立责任链链条
$handle1 = new handle1();
$handle1->push('handle2');
$handle1->push('handle3');
$handle1->action([1]);
$handle1->action([1, 2]);
$handle1->action([1, 2, 3]);
Пример #2
0
    {
        return __FUNCTION__;
    }
}
class handle1 extends handle
{
    protected function a()
    {
        return __CLASS__ . __FUNCTION__;
    }
    protected function b()
    {
        return __CLASS__ . __FUNCTION__;
    }
}
class handle2 extends handle
{
    protected function a()
    {
        return __CLASS__ . __FUNCTION__;
    }
    protected function b()
    {
        return __CLASS__ . __FUNCTION__;
    }
}
$handle1 = new handle1();
$handle2 = new handle2();
$handle1->action();
$handle2->action();
//it is so esay