导航类PHP

时间:2012-11-05 11:36:55

标签: php

您好我正在学习PHP中的OOP编程,我正在尝试创建一个Navigation类(具有正确的OOP结构),但不知怎的,我无法使其工作。我不确定这些课程是错的还是我以错误的方式打电话给他们..有人可以看看并帮我解决这个问题:)请参阅下面的代码

导航类:

<?php
class Navigation implements navigationInterface{

    public $menu = null;
    public $name = null;
    public $klasse = null;
    public $items = null;

    public function __construct($name, $klasse) {

        $this->name = $name;
        $this->klasse = $klasse;

    }

    public function addChild(NavigationItem $item)
    {
        $this->items[] = $item;
    }

    public function getName() {
        return $this->name;
    }

    public function getClass() {
        return $this->klasse;
    }


    public function display() {

        $menu = '<nav class="' . $this->getName() . '"><ul>';

        foreach ($this->items as $item) {

           $menu.= $item->display();

        }

        $menu .= '</ul></nav>';

        return $menu;

    }
}
?>

和我的导航项类(添加项目)

<?php
class NavigationItem implements NavigationItemInterface {

    public $menu = null;

    public function setMenuItem($items) {

        $this->menuItem = $items;
    }

    public function getMenuItem() {
        return $items;
    }

    public function display() {


        return '<li><a class="' . $this->getClass() . '" href="index.php?page=' . $this->getMenuItem() . '.php">' . $this->getMenuItem() . '</a></li>';

    }

}
?>

以下是我应该如何打电话给班级

<?php
$menu = new Navigation("navigation", "mainmenu");
$items = new NavigationItem();
$menu_items = array("home", "about", "playground");
$items->setMenuItem($menu_items);
$menu->addChild($items);
echo $menu->display();

?>

1 个答案:

答案 0 :(得分:0)

问题是'它不起作用'。试试这个。顺便说一下导航课那些公共领域应该是私有的。

<?php
   $menu = new Navigation("navigation", "mainmenu");
   $menu_items = array("home", "about", "playground");
   foreach($menu_items as $item) 
   { 
   $items = new NavigationItem();
   $items->setMenuItem($item);
   $menu->addChild($items);
    }
  echo $menu->display();

&GT;