用接口替换抽象类

时间:2014-07-29 08:21:41

标签: php class

我正在努力分支现有的MVC PHP应用程序,并且所有普通页面控制器都是从控制器扩展的。

class MyController extends Controller

控制器类是一个抽象类,包含2种魔术方法,__set__get以及构造函数__construct

abstract class Controller {
    protected $registry;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function __get($key) {
        return $this->registry->get($key);
    }

    public function __set($key, $value) {
        $this->registry->set($key, $value);
    }
}

我搜索了整个应用程序,并且无法手动调用__get__set。因此,我必须假设这些方法确实很神奇,并且正在使用无法查看的注册表执行某些操作。

通过Front类完成调度,该类已将注册表作为对象保存:

final class Front {
    private $registry;
    private $pre_action = array();
    private $error;

    public function __construct($registry) {
        $this->registry = $registry;
    }

    public function addPreAction($pre_action) {
        $this->pre_action[] = $pre_action;
    }

    public function dispatch($action, $error) {
        $this->error = $error;
        foreach ($this->pre_action as $pre_action):
            $result = $this->execute($pre_action);
            if ($result):
                $action = $result;
                break;
            endif;
        endforeach;

        while ($action):
            $action = $this->execute($action);
        endwhile;
    }

    private function execute($action) {
        $result = $action->execute($this->registry);

        if (is_object($result)):
            $action = $result;
        elseif ($result === false):
            $action = $this->error;
            $this->error = '';
        else:
            $action = false;
        endif;

        return $action;
    }
}

我的想法是用接口替换抽象类Controller是一个更好的解决方案,但我不确定这些魔术方法实际上在做什么。

如何判断我是否可以进行此更改?

0 个答案:

没有答案