Php 这个OOP代码基本正确吗?

Php 这个OOP代码基本正确吗?,php,oop,data-structures,object-oriented-analysis,Php,Oop,Data Structures,Object Oriented Analysis,我目前正在尝试将我们的页面模板转换为OOP,我觉得我为导航类提出的方法在根本上并不正确 这些方法中的一些真的属于drawNav的扩展类吗 getMenuBar->generateMenuBar->generateMenuItems结构是否分解过多?它应该是getMenuBar,并将generateMenuBar()和generateMenuItems()中的所有内容放入getMenuBar() 我调用类和方法的方式: $drawNav = new drawNav(); $breadcrum

我目前正在尝试将我们的页面模板转换为OOP,我觉得我为导航类提出的方法在根本上并不正确

  • 这些方法中的一些真的属于
    drawNav
    的扩展类吗
  • getMenuBar
    ->
    generateMenuBar
    ->
    generateMenuItems
    结构是否分解过多?它应该是
    getMenuBar
    ,并将
    generateMenuBar()
    generateMenuItems()
    中的所有内容放入
    getMenuBar()
我调用类和方法的方式:

$drawNav = new drawNav();

$breadcrumbTrail = $drawNav->getBreadcrumbTrail();
$menuBar = $drawNav->getMenuBar();
守则:

class drawNav { 

            public function __construct() {
                //I’ve not nothing to put here…                                
            }

            public function getMenuBar()
            {
               return $this->generateMenuBar();                          
            }

            public function getBreadcrumbTrail()
            {
                return $this->generateBreadcrumbTrail();                           
            }

            public function getSocialMediaButtons()
            {
                return $this->generateSocialMediaButtons();    
            }

            private function generateSocialMediaButtons()
            {
               //return the HTML code with the social media buttons
            }

            private function generateMenuBar()
            {
               //Generate the HTML containing the menu and social media buttons
               $this->generateMenuItems();
               $this->getSocialMediaButtons();
               //Generate the HTML closing tags for the container for the menu and social media buttons
            }

            private function generateMenuItems()
            {
                //Call to the database and generate each individual menu item and its dropdown
            }

            private function generateBreadcrumbTrail()
            {
                //Generate the HTML containing the breadcrumb trail
                $this->generateBreadcrumbs();
                //Generate the HTML closing tags for the container for the breadcrumbtrail
               }

            private function generateBreadcrumbs()
            {
                //Call to the database and generate the pieces of the breadcrumb trail
            }
}
getMenuBar
->
generateMenuBar
->
generateMenuItems
结构是否分解过多

对。绝对没有理由使用单行公共方法的一对一映射来包装私有方法。这不在任何人的最佳OOP实践列表中

而不是这种奇怪:

        public function getSocialMediaButtons()
        {
            return $this->generateSocialMediaButtons();    
        }
        // ...
        private function generateSocialMediaButtons()
        {
           //return the HTML code with the social media buttons
        }
您只需执行以下操作:

        public function getSocialMediaButtons()
        {
            //return the HTML code with the social media buttons 
        }
如果您担心能够在公共接口中混合和匹配私有方法,那么在以后的日子里进行重构非常容易。但是编写单行公共方法,其唯一目的是调用具有几乎完全相同名称的私有方法,这是一个巨大的代码气味


否则,您的代码就可以了,但有一点需要注意:我希望您的“使用社交媒体按钮返回HTML代码”是在呈现一些外部HTML模板文件,并且您不是在类中内联编写HTML。后端/前端逻辑的良好分离比代码部分的结构更重要;我宁愿看到清晰地分离业务/视图逻辑的过程代码,也不愿看到精心编制的将它们混合在一起的面向对象代码。

我真的不认为如何分离方法有什么大问题。我个人更愿意让类方法处理一些特定的操作,然后使用其他方法将“构建块”方法组合成更复杂的操作。例如,如果您需要更改breadcrumb数据库逻辑,您只需在该方法中更改它,并且该方法从其他方法中提取出足够多的内容,以使它们不需要更改。

您所拥有的似乎很好


我想问您是否有导航类,因为这些都可以添加到导航类中,或者作为导航类的扩展,以保持清洁。

似乎这个问题在@Travesty3上会更好,我起初以为,但是在SOI上似乎有很多类似的问题如果你的方法都不依赖于类的实例,为什么不将它们设置为静态。@AlexLunix:哪里提到过没有一个方法依赖于类的实例?@Travesty3查看它们和它们的行为,似乎类更像是一种将它们组合在一起的方式,而不是一个需要实例的类。据我们所知,构造函数中也没有属性,我没有导航类,就是这样。那么,您是否建议将其称为导航而不是drawNav?不,您可以根据自己的喜好来称呼它,但命名建议您也有一个导航类。+1表示代码逻辑和模板的分离。