如何改进一个有太多重复变量的类?

时间:2011-03-10 14:53:32

标签: php variables

我正在构建我的网站,这是我的页面控制器,

/**
 * this file handles the retrieval and serving of page
 */ 
class controller_core 
{
    public $connection = null;
    public $page_model = null;
    public $authentication_admin = null;
    public $authentication_member = null;
    public $constant = null;

    public function __construct($connection)  
    {  
        $this->connection = $connection;
        $this->page_model = new __page_model($connection);
        $this->authentication_admin = new __authentication_admin($connection);
        $this->authentication_member = new __authentication_member($connection);
        $this->constant = new __constant_model($connection);
    } 

    public function render_page($authenticated_admin, $authenticated_member, $backbone)
    {
        # ob_start - Turn on output buffering. It lets you put output into a buffer instead of sending it directly to the client.
        # in computing, a buffer is a region of memory used to temporarily hold data while it is being moved from one place to another.
        ob_start();

        # set variable for users
        $user_primary = null;
        $user_secondary = null;
        $user_tertiary = null;
        $user_quartary = null;

        # set variable for members
        $member_primary = null;
        $member_secondary = null;
        $member_tertiary = null;

        # store the db connection object in the variable
        $connection = $this->connection;

        # determine it is a root user or a sub user from the class of authentication_admin
        $category_admin = $this->authentication_admin->get_admin_category($authenticated_admin);
        $category_member = $this->authentication_member->get_member_category($authenticated_member);

        # set either one of the user type to true
        switch($category_admin) 
        {
            case 'user_primary':
                $user_primary = true;
                break;
            case 'user_secondary':
                $user_secondary = true;
                break;
            case 'user_tertiary':
                $user_tertiary = true;
                break;
            case 'user_quartary':
                $user_quartary = true;
                break;
        }

        # set either one of the user type to true
        switch($category_member) 
        {
            case 'member_primary':
                $member_primary = true;
                break;
            case 'member_secondary':
                $member_secondary = true;
                break;
            case 'member_tertiary':
                $member_tertiary = true;
                break;  
        }

        # get the constant values from the class of constant
        $cst_value_site_title = $this->constant->get_constant('site_title')->cst_value;
        $cst_value_site_slogan = $this->constant->get_constant('site_slogan')->cst_value;
        $cst_value_meta_description = $this->constant->get_constant('meta_description')->cst_value;
        $cst_value_meta_keywords = $this->constant->get_constant('meta_keywords')->cst_value;


        # if $_REQUEST pg exists
        if(isset($_REQUEST['pg_url']))
        {
            # show the requested page
            # always send the value of $authentication_admin to the class of page:
            # if $authentication_admin has a value, you can see this page even if it is hidden
            # if $authentication_admin has a value, you can see this page only if it is published
            $page = $this->page_model->get_page($_REQUEST['pg_url'],$category_admin);
            $parent = $this->page_model->get_parent($page);

            # store the date into the variable
            $parent_id = $page->parent_id;
            $tmp_path = $page->tmp_path;

            # get the main template/ html document
            include $backbone;
            //print_r($authentication_admin);
        }
        else
        {   
            # if no special page is requested, we'll show the default page
            $page = $this->page_model->get_page(DEFAULT_PAGE,$category_admin);
            $parent = $this->page_model->get_parent($page);

            #store the date into the variable
            $parent_id = $page->parent_id;
            $tmp_path = $page->tmp_path;


            #get the main template/ html document
            include$backbone;
            #print_r($parent);
        }

        #Return the contents of the output buffer.
        return ob_get_contents();

        #Clean (erase) the output buffer and turn off output buffering.
        ob_end_clean();
    }
}
下面的

是从上面的父控制器类扩展的类,但是你可以看到我正在从父类重复一些(很多!)变量,

class controller_extended extends controller_core
    {
        function __construct($connection) 
        {
            parent::__construct($connection);
        }

        public function render_page($authenticated_admin, $authenticated_member, $backbone) 
        {
            # set variable for users
            $user_primary = null;
            $user_secondary = null;
            $user_tertiary = null;
            $user_quartary = null;

            # set variable for members
            $member_primary = null;
            $member_secondary = null;
            $member_tertiary = null;

            # store the db connection object in the variable
            $connection = $this->connection;

            # determine it is a root user or a sub user from the class of authentication_admin
            $category_admin = $this->authentication_admin->get_admin_category($authenticated_admin);
            $category_member = $this->authentication_member->get_member_category($authenticated_member);

            # if $_REQUEST tag_name exists 
            if(isset($_REQUEST['tag_name']))
            {
                # get the value from the request
                if(isset($_REQUEST['pg_url'])) $pg_url = $_REQUEST['pg_url'];
                if(isset($_REQUEST['tag_name'])) $tag_name = $_REQUEST['tag_name'];
                if(isset($_REQUEST['str_id'])) $str_id = $_REQUEST['str_id'];

                # show the requested page
                # always send the value of $authentication_admin to the class of page:
                # if $authentication_admin has a value, you can see this page even if it is hidden
                # if $authentication_admin has a value, you can see this page only if it is published
                $page = $this->page_model->get_page($pg_url,$category_admin);
                $parent = $this->page_model->get_parent($page);

                if(empty($str_id))
                {
                    # get the included template
                    switch($pg_url) 
                    {
                        case 'publications':
                            $tmp_path = 'resources_publication_subitem.php';
                            break;
                        case 'tender-opportunities':
                            $tmp_path = 'resources_tender_opportunitie_subitem.php';
                            break;
                        case 'research-topics':
                            $pg_url = $tag_name;
                            $tmp_path = 'item_content_research_topics.php';
                            break;
                        case 'videos':
                            $tmp_path = 'video_tagged.php';
                            break;
                        case 'forum':
                            $tmp_path = 'forum_subitem.php';
                            break;  
                        case 'ener-exchange':
                            $tmp_path = 'exchange_subitem.php';
                            break;
                    }
                }
                else
                {
                    # store the date into the variable
                    $parent_id = $page->parent_id;

                    # get the included template
                    switch($pg_url) 
                    {
                        case 'forum':
                            $tmp_path = 'item_forum.php';
                            break;  
                        case 'ener-exchange':
                            $tmp_path = 'item_exchange.php';
                            break;
                    }

                }

                # get the main template/ html document
                include $backbone;

            }
            else
            {
                parent::render_page($authenticated_admin, $authenticated_member, $backbone);
            }
        }
    }

如何处理这些重复变量?也许我错误地控制了控制器?

感谢。

修改

抱歉不清楚。我的扩展类中的render_page方法覆盖了父类中的render_page方法,因此我认为我无法使用parent关键字来获取我存储在其中的变量render_page方法。我该怎么办?感谢。

1 个答案:

答案 0 :(得分:-1)

很难了解这些变量在部署中的目的是什么,但仅仅从查看这里有什么可以考虑的事情。

您可以将用于确定用户级别的开关放在父类的受保护方法中。这样,用于检查用户级别的逻辑可以由任何后代控制器中的render_page调用。您可以传入get_admin_category中的值并返回一个字符串,以便可以重用switch语句。

此外,您的示例中输出缓冲背后的意图是什么?我发现你最后在include $backbone包含了你的模板,但是你为什么要做缓冲呢?在大多数发布的MVC中,控制器执行一些预处理,调用模型方法并排列数据,然后将其传递给视图,这是向客户端呈现的唯一内容(例如,我知道CodeIgniter通过传递'来实现这一点'输出'数组到视图,可以包含通过回显等输出的值)。不应该将输出延迟到客户端,因为在大多数MVC中,只有在加载视图后才会出现这种情况。

从你使用$ _REQUEST超全局的外观来看,你依靠这个来执行请求到服务器上实际脚本的路由(我可能会误解,只是试图解释给出的内容。)许多MVC还将利用mod_rewrite()执行到一个入口点(前端控制器)的所有路由,然后依靠脚本通过调用url中路径的子构造函数来处理所有路由。这是一个很好的练习教程,而不是部署,目的: http://www.phpaddiction.com/tags/axial/url-routing-with-php-part-one/

如果您是在理解Front Controller模式的练习中,那么您可以查看很多很棒的框架并从中学习一些技巧。 CodeIgniter和Kohanna非常棒,相对轻巧,可以很容易地看到引擎盖下。显然,Zend也非常受欢迎。