1 Replies - 241 Views - Last Post: 15 November 2017 - 05:23 AM Rate Topic: -----

#1 salv236  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 04-January 15

unable to print an array within a class when instantiating an object

Posted 15 November 2017 - 05:14 AM

Hi folks,

I am weak at OOP, i am encountering issues when printing out the output of an array, i receive a message 'Array to string conversion in '
I tried to use the implode function to covert an array into a string, i had a look at the documentation on php.net on the arguments it takes.
I cannot see any errors.

For each object that i instantiate from the Product class i would like to print out for each object each product from the array, below is the code:

<?php

  class Shop {
  private $companyName;
  private $address;
  private $owner;

  public function __construct($companyName, $address, $owner){
    $this->companyName = $companyName;
    $this->address = $address;
    $this->owner = $owner;
  }

  public function getCompanyName(){
    return $this->companyName;
  }

  public function getAddress(){
    return $this->address;
  }

  public function getOwner(){
    return $this->owner;
  }

  public function shopOutput(){
    echo '<strong>' . strtoupper($this->companyName) . ':' . '</strong>' . ' is located at ' . $this->address . 'the owner is ' . $this->owner;
  }

}

  class Product {

  private $item;
  private $packagingType;
  private $quantity;


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

  }

  public function getItem(){
    $this->item;
    return $item = array('milk', 'biscuits', 'coffee', 'sugar');
      foreach($item as $brand){
        echo $brand;
        implode(" ", $brand);
      }
  }

  public function getPackage(){
    return $this->packagingType;
  }

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

public function productOutput(){
  echo ' item offered is ' .$this->item . ',' . ' it is wrapped in a ' . $this->packagingType . ',' . ' you ordered ' . $this->quantity;

}


}

$shop1 = new Shop('walmart', '365 Brooklyn Av, NYC ', 'Derick Corby');
$shop2 = new Shop('costco', '320 University Avenue, Toronto ', 'Cedric Williams');
$shop3 = new Shop('starbucks', 'Rue de Progres 57, Brussels ', 'Pieter Geeraerts');

echo $shop1->getCompanyName() . '<br>';
echo $shop1->getAddress() . '<br>';
echo $shop1->getOwner() . '<br><br>';

echo $shop1->shopOutput() . '<br>';
echo $shop2->shopOutput() .'<br>';
echo $shop3->shopOutput() . '<br><br>';

//product specifications

$product1 = new Product([0], 'carton', '1' );

echo $product1->productOutput();





Is This A Good Question/Topic? 0
  • +

Replies To: unable to print an array within a class when instantiating an object

#2 Dormilich  Icon User is offline

  • 痛覚残留
  • member icon

Reputation: 4142
  • View blog
  • Posts: 13,098
  • Joined: 08-June 10

Re: unable to print an array within a class when instantiating an object

Posted 15 November 2017 - 05:23 AM

Quote

i am encountering issues when printing out the output of an array, i receive a message 'Array to string conversion in '

The offending line is
echo ' item offered is ' .$this->item . ',' . ' it is wrapped in a ' . $this->packagingType . ',' . ' you ordered ' . $this->quantity;


It errors out because, well, you try to print an array.

Quote

I tried to use the implode function to covert an array into a string

But you never change the array you want to print (i.e. $this->item).

Note:
  public function getItem(){
    // does nothing
    $this->item;
    // returns the written array no matter what the original array is
    return $item = array('milk', 'biscuits', 'coffee', 'sugar');
    // is never executed since the method already returned
      foreach($item as $brand){
        echo $brand;
        // strings cannot be imploded
        implode(" ", $brand);
      }
  }


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1