简体   繁体   中英

PHP returning data from a collection of objects

I am trying to create a collection of Item objects and display them on the screen though an object oriented programming style. I have two classes called LineItem and ObjectCollection. LineItem class holds each item object and ObjectCollection holds the data of collection of all the lineitems. Following code shows my LineItem class and the ObjectCollection class.

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;
  }
}

ObjectCollection:

 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;
  }

}

Then I added further code to add 2 new items to the LineItem. At the same time, I added those results into my object collection.

  $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);

When I try to display each line time separately by typing,

print $lineitem1; 

It displays the correct result.

However, if I try to display items in the ObjectCollection class, it does not display any result on the screen.

This is the code that I am using to display my Object Collection;

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 />";
  }

What alterations should I make to my code so I can display my collection of objects?

Your ObjectCollection::getLineItem method returns array of items .

It doesn't process passed parameter.

In a simpliest case method getLineItem should be rewritten to:

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

Another option is use foreach and leave getLineItem as is:

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

Without changing anything else, this solves your problem:

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

Improved legibility

If you prefer your code more legible, use sprintf() instead:

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()
    );
}

For reference, see http://php.net/manual/en/function.sprintf.php .

Unrelated

The local variable $lineCounter in ObjectCollection::getLineCount() is undefined, you should (as commented out) return the instance field instead:

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

Use print_r($ca) or var_dump($ca) to see data contained by the object.

print_r prints recursively (usefull for arrays) var_dump shows content of an object

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM