Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Разработчикам на заметку: общий родитель для контроллеров и моделей


Recommended Posts

Мне нравится структурный подход. И когда несколько классов имеют повторяющийся код, то для меня это повод задуматься - а не стоит ли вынести этот код в общего родителя?

И вот что я сделал.

Создал класс Object (файл object.php в /system/engine):

<?php

abstract class Object
{

    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);
    }

}

// EOF
И классы Model и Controller наследовал от него:

abstract class Model extends Object
{
}
abstract class Controller extends Object
{

    protected $id;
    protected $template;
    protected $children = array();
    protected $data = array();
    protected $output;

    public function __construct($registry)
    {
        parent::__construct($registry);

        $this->data['charset'] = $this->language->get('charset');
        $this->data['lang'] = $this->language->get('code');
        $this->data['direction'] = $this->language->get('direction');
        $this->data['template'] = $this->config->get('config_template');
        $this->data['store'] = $this->config->get('config_name');
        $this->data['t'] = $this->language;
    }
}
По побльшому счету, на работу это никак не вдияет, но если теперь нам вдруг потребуется как-то иначе построить логику работы с регистром (т.е. как-то иначе обрабатывать геттеры и сеттеры, то мы будем менять только класс Object.
  • +1 1
Link to comment
Share on other sites


Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

On our site, cookies are used and personal data is processed to improve the user interface. To find out what and what personal data we are processing, please go to the link. If you click "I agree," it means that you understand and accept all the conditions specified in this Privacy Notice.