PHP分层导航OOP结构 [英] PHP Hierarchical Navigational OOP structure

查看:69
本文介绍了PHP分层导航OOP结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用PHP表示树形导航结构的最佳方法是什么?我需要能够使用面包屑痕迹来跟踪树中的当前页面位置.树的一部分是从数据库(例如make->模型)生成的.每个模型都具有相同的等效树分支,例如:

What is the best way to represent a tree navigation structure using PHP? I need to be able to track the current page location in the tree using a bread crumb trail. Parts of the tree are generated from a database such as make -> models. Each model then has the same equivalent tree branch, such as:

制作>模型>(区域1,区域2,区域3).

Make > Model > (Area 1, Area 2, Area 3).

树的某些部分可能会随时间变化.最好是使用静态的类层次结构或动态的解决方案来重用类?

Parts of the tree may change over time. Is it best to have a static class hierarchy structure or a dynamic solution where classes are re-used?

希望这已经被简单解释了.

Hope this has been explained simply.

推荐答案

从面向对象的角度来看,我建议定义一个接口,如下所示:

From an OO point of view, I'd recommend defining an interface such as the following:

interface BreadcrumbInterface
{
    public function getLabel();

    public function getParent(); // returns an instance of BreadcrumbInterface, or null
}

然后,您将创建一个Page类,该类实现此接口,并且可以选择包含一个父级",该父级也必须实现此接口.这将建立您需要的层次结构.

Then you'd create a Page class, which implements this interface and can optionally contain a 'parent', which must also implement this interface. This will build the heirarchy that you need.

检索完整面包屑的一种好方法(在此过程中仔细研究OO设计模式)是使用访客模式.在这种情况下,您可能想要定义一个通用的抽象类以及接口,以便抽象"处理访问者的逻辑.

A great way to retrieve the full breadcrumbs (while brushing up on your OO design patterns in the process) is to use the visitor pattern. In this case, you probably want to define a common abstract class as well as the interface in order to 'abstract away' the logic of handling the visitor...

abstract class BaseNode implements BreadcrumbInterface
{
    protected $parent = null;

    public function accept(BreadcrumbVisitor $visitor)
    {
        $visitor->visit($this);
    }

    public function setParent(BreadcrumbInterface $parent)
    {
        $this->parent = $parent;
    }

    public function getParent()
    {
        return $this->parent;
    }
}

class BreadcrumbVisitor
{
    protected $breadcrumbs = array();

    public function visit(BreadcrumbInterface $node)
    {
        $parent = $node->getParent();
        if ($parent instanceof BaseNode) {
            $parent->accept($this);
        }

        $this->breadcrumbs[] = $node->getLabel();
    }

    public function getBreadcrumbs()
    {
        return $this->breadcrumbs;
    }
}

这不会按原样运行,但希望您能理解.您可能还希望节点确定其页面的URL以及标签,但这可以很容易地添加.我只是想展示解决此问题的一般面向对象结构.

This won't run as is, but hopefully you get the idea. You probably also want your nodes to determine the URL to their page as well as the label, but that can be added easily enough. I just wanted to show the general OO structure to solving this problem.

添加粗略用法示例:

$rootPage = new Page(/*...*/);

$parentPage = new Page(/*...*/);
$parentPage->setParent($rootPage); // In reality you most likely wouldn't be building this structure so explicitly. Each object only needs to know about it's direct parent

$currentPage = new Page(/*...*/);
$currentPage->setParent($parentPage);

$visitor = new BreadcrumbVisitor();
$currentPage->accept($visitor);
$breadcrumbs = $visitor->getBreadcrumbs(); // returns an array, where the first element is the root

// then you can implode with ' > ' if you want
$breadcumbString = implode(' > ', $breadcrumbs);

这篇关于PHP分层导航OOP结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆