【问题标题】:PHP returning data from a collection of objectsPHP 从对象集合中返回数据
【发布时间】:2015-12-28 18:48:14
【问题描述】:

我正在尝试创建 Item 对象的集合,并通过面向对象的编程风格将它们显示在屏幕上。我有两个名为 LineItem 和 ObjectCollection 的类。 LineItem 类保存每个项目对象,ObjectCollection 保存所有 lineitem 的集合数据。以下代码显示了我的 LineItem 类和 ObjectCollection 类。

class LineItem {
  private $item;
  private $quantity;

  public function __construct($item, $quantity) {
    $this->item = $item;
    $this->quantity = $quantity;
  }

  public function __toString() {
    return "Item = ".$this->item." : Quantity =  "
    .$this->quantity;
  }

  public function setQuantity($quantity) {
      $this->quantity = $quantity ;
  }

  public function getQuantity(){
      return $this->quantity;
  }

  public function changeQuantity($value){
    $this->quantity += $value;
  }

  public function getItem(){
    return $this->item;
  }
}

对象集合:

 class ObjectCollection  {  
  //This is an array to hold line items
  private $line_items_array ;  
  private $lineCounter; //Count the number of line items

  public function __construct() {
      //Create an array object to hold line items
      $this->line_items_array = array();
      $this->lineCounter = 0; 
  }

  // This will add a new line object to line items array
  public function addLineItem($line_item) {
    $this->lineCounter++;
    $this->line_items_array[] = $line_item;
  }

  public function getLineCount(){
    return $lineCounter;
    //return $this->lineCounter;
  } 

  public function getLineItem(){
    return $this->line_items_array;
    //return $line_items_array;
  }

}

然后我添加了更多代码以将 2 个新项目添加到 LineItem。同时,我将这些结果添加到我的对象集合中。

  $ca = new ObjectCollection();

  $item1 = new Item("1",3.45);
  $item1->setDescription("Description for Item 1");
  $item1->setImage("Image1");
  $lineitem1 = new LineItem($item1, 5);

  $item2 = new Item("2",5.31);
  $item2->setDescription("Description for Item 2");
  $item2->setImage("Image2");
  $lineitem2 = new LineItem($item2, 8);


  $ca->addLineItem($lineitem1);
  $ca->addLineItem($lineitem2);

当我尝试通过键入分别显示每一行时间时,

print $lineitem1; 

它显示正确的结果。

但是,如果我尝试在 ObjectCollection 类中显示项目,它不会在屏幕上显示任何结果。

这是我用来显示我的对象集合的代码;

for ($i = 0; $i < $ca->getLineCount(); $i++) {
      $li = $ca->getLineItem($i);
      $item = $li->getItem();
      print $i.")Description:" . $item->getDescription() . ", 
  Price: ". $item->getPrice() . ", Quantity:" . $li->getQuantity() . "<br />";
  }

我应该对我的代码进行哪些更改才能显示我的对象集合?

【问题讨论】:

标签: php oop


【解决方案1】:

您的ObjectCollection::getLineItem 方法返回项目数组

它不处理传递的参数。

在最简单的情况下,getLineItem 应该重写为:

public function getLineItem($index) {
    return $this->line_items_array[$index];
}

另一种选择是使用foreach 并保持getLineItem 不变:

foreach ($ca->getLineItem() as $li) {
    print $li;
}

【讨论】:

  • 我应该对我的代码进行哪些更改以便处理传递的参数
【解决方案2】:

无需更改任何其他内容,即可解决您的问题:

foreach ($ca->getLineItem() as $i => $li) {
    $item = $li->getItem();
    print ($i + 1).")Description:" . $item->getDescription() . ",
  Price: ". $item->getPrice() . ", Quantity:" . $li->getQuantity() . "<br />";
}

提高了易读性

如果您希望代码更清晰易读,请改用sprintf()

foreach ($ca->getLineItem() as $i => $li) {
    $item = $li->getItem();
    print sprintf(
        "%s)Description:%s, Price: %s, Quantity:%s<br />",
        $i + 1,
        $item->getDescription(),
        $item->getPrice(),
        $li->getQuantity()
    );
}

有关参考,请参阅http://php.net/manual/en/function.sprintf.php

不相关

ObjectCollection::getLineCount() 中的局部变量 $lineCounter 未定义,您应该(如注释掉)返回实例字段:

public function getLineCount()
{
    return $this->lineCounter;
}

【讨论】:

    【解决方案3】:

    使用print_r($ca)var_dump($ca) 查看对象包含的数据。

    print_r 递归打印(对数组有用) var_dump 显示对象的内容

    【讨论】:

      猜你喜欢
      • 2021-07-28
      • 2021-04-11
      • 2020-01-05
      • 2014-12-03
      • 1970-01-01
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多