【发布时间】:2012-08-29 18:14:43
【问题描述】:
我目前正在尝试将我们的页面模板转换为 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
}
}
【问题讨论】:
-
似乎这个问题在codereview.stackexchange.com上会更好
-
@Travesty3 一开始我也是这么想的,但是好像SO上有很多类似的问题
-
如果您的方法都不依赖于类的实例,为什么不将它们设为静态。
-
@AlexLunix:在哪里提到没有任何方法依赖于类的实例?
-
@Travesty3 看看它们以及它们的作用,似乎类更像是一种将它们组合在一起的方式,而不是需要实例的类。据我们所知,这里也没有属性,构造函数中也没有。
标签: php oop data-structures object-oriented-analysis