在 PHP 类中使用数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13220982/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:00:21  来源:igfitidea点击:

Using Array in PHP Class

phpclass

提问by Reshad

I am exercising some PHP OOP and therefore I am creating a class to create a simple navigation menu ( with extensions in the future ) now I have build this class that works kinda.. with 1 menu item tough.. I don;t know how to use arrays in my class to use the class like

我正在练习一些 PHP OOP,因此我正在创建一个类来创建一个简单的导航菜单(将来会有扩展),现在我已经构建了这个类,它的工作原理有点......有 1 个菜单项很难......我不知道如何在我的类中使用数组来使用类

<?php
$menu = new Navigation("Test", "mainmenu");

$menu->setMenuItem("home", "test");
echo $menu->display();

?>

as you see I should be able to give each menu item with the setMenuItem(); method. but since it does not use Arrays at the moment I only get the first value

如您所见,我应该能够使用 setMenuItem() 来提供每个菜单项;方法。但由于它目前不使用数组,我只得到第一个值

The class itself is as follows:

类本身如下:

<?php
class Navigation implements navigationInterface {

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

    public function __construct($name, $klasse) {

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

    }

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

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

    public function setMenuItem($items) {
        $this->menuItem = $items;
    }

    public function getMenuItem() {
        return $this->menuItem;
    }

    public function display() {

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

        return $menu;

    }
}
?>

who can show me how to use arrays within the class in combination with a loop to create a menu with all given values?

谁能告诉我如何将类中的数组与循环结合使用来创建具有所有给定值的菜单?

Thanks in advance.

提前致谢。

回答by Vikas Umrao

<?php
class Navigation{

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

    public function __construct($name, $klasse) {

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

    }

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

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

    public function setMenuItem($items) {

        $this->menuItem = $items;
    }

    public function getMenuItem($menu) {
        return $menu;
    }

    public function display() {

        $menu = '<nav class="' . $this->getName() . '">';
        if(is_array($this->menuItem))
        {
        foreach($this->menuItem as $val)
        {
        $menu .= '<li><a class="' . $this->getClass() . '" href="index.php?page=' . $this->getMenuItem($val) . '.php">' . $this->getMenuItem($val) . '</a></li>';
        }
        }
        else{
            $menu .= '<li><a class="' . $this->getClass() . '" href="index.php?page=' . $this->getMenuItem($this->menuItem) . '.php">' . $this->getMenuItem($this->menuItem) . '</a></li>';

        }


        $menu .= '</nav>';

        return $menu;

    }
}
?>

<?php
$menu = new Navigation("Test", "mainmenu");

$menu_items=array("home","test");
$menu->setMenuItem($menu_items);

echo $menu->display();

?>

回答by hakre

You actually have two types, not one:

你实际上有两种类型,而不是一种:

  1. MenuItemList
  2. MenuItem
  1. MenuItemList
  2. MenuItem

The MenuItemListwould take care of managing the list. It coulduse an array internally. A code example for something very similar could be found in a previous answer: Array Object In Php.

MenuItemList会采取管理列表的照顾。它可以内部使用数组。在之前的答案中可以找到非常相似的代码示例:Array Object In Php

Next to that the display()method does not belong into the two. Instead you should make your template that keen it knows how to output a menu list:

其次该display()方法不属于这两种。相反,您应该让您的模板敏锐地知道如何输出菜单列表:

echo '<ul>';
foreach ($menu as $item) {
   echo '<li class="menutitem ', $item->getClass(), '"><a href="index.php?page=', ...;
}
echo '</ul>';

This would also allow you to keep some procedural knowledge which often is more straight forward with the templating while knowing that you menu model just works and is properly encapsulated.

这也将允许您保留一些程序知识,这些知识通常在模板中更直接,同时知道您的菜单模型可以正常工作并且被正确封装。

回答by Aliweb

do like this :

这样做:

<?php
class Navigation implements navigationInterface {

    public $menu = array();
    public $name = null;
    public $klasse = null;

    public function __construct($name, $klasse) {

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

    }

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

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

    public function setMenuItem($name , $value) {
        $this->menu[$name] = $value;
    }

    public function getMenuItem($name) {
        return $this->menu[$name];
    }

    public function display() {

        if(!empty($this->menu)){

            $menu .= "<ul>";

            foreach($this->menu as $key => $value){
                $menu .= "<li><a href='".$value."'>$key</a></li>";
            }

            $menu .= "</ul>";
        }
        return $menu;
    }
}
?>

I think this is what you need

我认为这就是你所需要的

回答by Martin Lyne

At top of your class

在您的班级中名列前茅

$menuItems = array();

then in setMenuItem()

然后在 setMenuItem()

function setMenuItem($name, $value)
{
  $this->menuItems[] = array($name, $value);
}

then

然后

function display()
{
  // Your other code here
  foreach($this->menuItems as $item)
  {
    echo '<li><a href="'.$item[1].'">'.$item[0].'</a></li>';
  }
  // Your other code here
}

Hope that helps.

希望有帮助。

Edit:You could change the way they are stored, and prevent duplicates by using the nav "Name" as the key it is stored under, but I've kept it simple for ease of understanding.

编辑:您可以更改它们的存储方式,并通过使用导航“名称”作为其存储的键来防止重复,但为了便于理解,我保持简单。

Heres a more advanced sample

这是一个更高级的示例

function setMenuItem($name, $value)
{
  if(!in_array(array_keys($this->menuItems)), $name) {
    $this->menuItems[$name] = $value;
  } else {
    echo "That's already been added!"; //Handle this properly though
  }
}

then

然后

function display()
{
      // Your other code here
      foreach($this->menuItems as $name=>$value)
      {
        echo '<li><a href="'.$value.'">'.$name.'</a></li>';
      }
      // Your other code here
}