1

I have an array of Product IDs and want to get the product information by calling the Product class. I have tried adding it in a foreach loop and get a fatal error saying Object of class Product could not be converted to string. This is what I have tried.

$productIDs = Db::getInstance()->executeS("SELECT id_product FROM wtop_product ORDER BY position ASC");

$products = '';
foreach($productIDs as $productID)
{
    $products .= new Product($productID['id_product'], false, '1');
}

Since this ouputs an error obviously it is not the correct way to handle this situation. What i'm not sure of is how to pass the array of product IDs to the new Product call and get an output of each of those products.

4
  • 2
    Should you not be building a list of objects ? Commented Nov 19, 2013 at 22:35
  • 1
    To expand on karthikr's comment, try $products[] = ... to create an array of objects, rather than gluing them together as strings. Commented Nov 19, 2013 at 22:37
  • Should new Product return a string? If so implement a __toString function Commented Nov 19, 2013 at 22:39
  • Have you tried making a toString() method for your class and calling (new Product(...))->toString();? Commented Nov 19, 2013 at 22:39

1 Answer 1

1

Store objects in array:

$products = array();
foreach($productIDs as $productID) {
    $products[$productID['id_product']] = new Product($productID['id_product'], false, '1');
}
print_r($products);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.