strategies for managing long class files in php

前端 未结 6 804
野的像风
野的像风 2021-02-09 08:37

I\'ve got a bunch of functions that I want to move into a class. They\'re currently split into a couple of fairly long files. I\'d prefer not to have one 2500 line file, but as

6条回答
  •  逝去的感伤
    2021-02-09 09:00

    They are all in different files, which means that they were different enough to group by file. Just take the same logic when building them into classes. I have a Page object that deals with building the page. Technically the HTML for my page header is part of the page, but I separate it into a Page_HTML class for maintaining my sanity and not creating gigantic classes.

    Also, I tend to make the sub_classes, like Page_HTML in this case, static, instead of instantiating it. That way I can access the $this variables in the Page class, but still group it into another class.

    class Page
    {
        function buildPage($content)
        {
            $content = Page_HTML::getHeader() . $content;
        }
    }
    
    class Page_HTML
    {
        function getHeader()
        {
        }
    }
    

提交回复
热议问题